我想创建诸如{id,project_id}之类的复合主键。我删除旧表(全部)。当我这样做时:
AssertionError: Model mdm.Group can't have more than one AutoField.
我有一个错误:
id = models.AutoField(db_index=True, primary_key=False)
更改我的模型:
constraints = [
models.UniqueConstraint(
fields=['id', 'project_id'], name='unique_group_project'
)
]
并将复合主键添加为
By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
This is an auto-incrementing primary key.
If you’d like to specify a custom primary key, just specify primary_key=True on one of your fields. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
来自文档:
{{1}}
我只是不明白问题所在。如果添加AutoField,则必须为PK。如何解决Autofield ID和复合PK(ID,project_id)的问题?