Django模型有两个主键

时间:2014-10-28 22:01:07

标签: django python-2.7 django-rest-framework

我在表中有两个主键我知道django在同一个表中不支持多个PK 我使用unique_identifier但是当我执行syncdb时,所有列都没有创建

class SODIOrder(models.Model):
    sodi_order_num = models.CharField(max_length=50, primary_key=True)
    sodi_order_version = models.CharField(max_length=50,primary_key=True)
    service_identifier = models.CharField(max_length=50)
    telephone_number = models.CharField(max_length=12)
    insert_timestamp = DateTimeUTCField(auto_now_add=True)

    class Meta:
        unique_together = (("sodi_order_num","sodi_order_version"))
没有创建

sodi_order_version任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:7)

从模型字段中删除primary_key=True并将unique_together更改为tuple of tuples,如下所示:

class SODIOrder(models.Model):
    sodi_order_num = models.CharField(max_length=50)
    sodi_order_version = models.CharField(max_length=50)
    service_identifier = models.CharField(max_length=50)
    telephone_number = models.CharField(max_length=12)
    insert_timestamp = DateTimeUTCField(auto_now_add=True)

    class Meta:
        unique_together = (("sodi_order_num","sodi_order_version"),)