Two primary keys specified in MySQL database

人盡茶涼 提交于 2019-11-30 14:31:06

It's a composite primary key. Try executing this:

show create table mytable;

It should show you the definition of the composite key.

This is nothing "unusual" from a mysql perspective.

It's not supported in Django, but there's a workaround. On your model specify unique_together and the fields in the Meta section:

class MyClass(models.Model):
    IDENTIFIER = models.IntegerField(blank=False,null=False)
    TIMESTAMP_ = models.IntegerField(blank=False,null=False)
    VALUE_ = models.TextField(blank=True, null=True)

    class Meta:
        unique_together = ('IDENTIFIER', 'TIMESTAMP_')

This will preserve the two-column primary key behavior.

Django currently does not support multi-column primary keys though there are patches/forks that extend it to do so (with varying degrees of polish). From the FAQ "Do Django models support multiple column primary keys?":

No. Only single-column primary keys are supported.

But this isn't an issue in practice, because there's nothing stopping you from adding other constraints (using the unique_together model option or creating the constraint directly in your database), and enforcing the uniqueness at that level. Single-column primary keys are needed for things such as the admin interface to work; e.g., you need a simple way of being able to specify an object to edit or delete.

What you're seeing is not two primary keys, but rather a two-column primary key. Tables by definition can only have one primary key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!