复合主键,Google应用引擎(django)

时间:2022-09-11 19:22:00

I use django nonrel/djangoappengine on google app engine. It is not possible to specify composite primary keys directly. However it should be possible to emulate this behaviour. I am wondering about what is the best approach.

我在谷歌应用引擎上使用django nonrel / djangoappengine。无法直接指定复合主键。但是,应该可以模拟此行为。我想知道什么是最好的方法。

Problem

问题

Conside the following datamodel

考虑以下数据模型

class AccumuatedSales(models.Model):
    salesman = models.ForeignKey(Salesman)
    year = models.Postiveinteger()
    totalSales = models.PositiveInteger()

I want (salesman, year) to be treated as a primary key. That is, if I 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. So the second save overwrites the first.

'table'AccumulatedSales应该包含一行。所以第二个保存会覆盖第一个保存。

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 this approach good or "the right one"?
  • 这种方法是好的还是“正确的”?
  • What is the best datatype for the field key? Personally I would like to have som 128-bit field, but that is not available to my knowledge.
  • 字段键的最佳数据类型是什么?就个人而言,我想拥有128位字符,但据我所知,这是不可用的。

1 个解决方案

#1


0  

This looks like a pretty good solution.

这看起来是一个非常好的解决方案。

#1


0  

This looks like a pretty good solution.

这看起来是一个非常好的解决方案。