Composite primary key, Google app engine (django) -


i use django nonrel/djangoappengine on google app engine. not possible specify composite primary keys directly. should possible emulate behaviour. wondering best approach.

problem

conside following datamodel

class accumuatedsales(models.model): salesman = models.foreignkey(salesman) year = models.postiveinteger() totalsales = models.positiveinteger() 

i want (salesman, year) treated primary key. is, if do

asby1 = accumulatedsales(salesman='joe', year=2010, totalsales=100) asby1.save() asby2 = accumulatedsales(salesman='joe', year=2010, totalsales=200) asby2.save() 

the 'table' accumulatedsales should contain one row. second save overwrites first.

possible solution

class accumuatedsales(models.model): key = models.charfield(primary_key=true,, default=none, editable=false) salesman = models.foreignkey(salesman) year = models.postiveinteger() totalsales = models.positiveinteger() def save(self): self.key = somehashfunction(self.salesman_id, self.year) super(accumulatedsales, self).save() 

questions

  • is approach or "the right one"?
  • what best datatype field key? have som 128-bit field, not available knowledge.

this looks pretty solution.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -