Composite primary key in django

前端 未结 3 463
孤独总比滥情好
孤独总比滥情好 2020-11-30 03:00

I have a legacy db table which has composite primary key. I don\'t think I will be able to change the structure to include a surrogate key, as there is some code written tha

3条回答
  •  野性不改
    2020-11-30 03:32

    The accepted answer is fine. However, it's a little old. unique_together may be deprecated in favor of UniqueConstraint. So, the better way of doing this would be;

    UniqueConstraint(fields = ['key1', 'key2'], name = 'constraint_name')
    

提交回复
热议问题