MySql table没有主键,django orm无法使用
MySql table do not has primary key, django orm can not use
我将 Django ORM 与 MySQL 数据库一起使用,但我有一些 table 没有主键。它抛出这样的异常:
django.db.utils.OperationalError: (1054, "Unknown column 'XXX.id' in 'field list'")
如何在不修改我的 table 架构的情况下修复异常?
不,您不能拥有没有主键的模型。
https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
Each model requires exactly one field to have primary_key=True
(either explicitly declared or automatically added).
我将 Django ORM 与 MySQL 数据库一起使用,但我有一些 table 没有主键。它抛出这样的异常:
django.db.utils.OperationalError: (1054, "Unknown column 'XXX.id' in 'field list'")
如何在不修改我的 table 架构的情况下修复异常?
不,您不能拥有没有主键的模型。
https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
Each model requires exactly one field to have
primary_key=True
(either explicitly declared or automatically added).