Django错误:““字段列表”中的未知列“ categorytree.id”” [英] Django Error: "Unknown column 'categorytree.id' in 'field list'"
问题描述
通过Django shell运行以下命令时:
When running the following command via the Django shell:
from myApp.models import *
print Categorytree.objects.all()
我收到此错误消息:
OperationalError:(1054,字段
列表中的未知列'categorytree.id')
OperationalError: (1054, "Unknown column 'categorytree.id' in 'field list'")
这是我的模型的样子:
class Categorytree(models.Model):
level = models.IntegerField(db_column='Level') # Field name made lowercase.
categorynode_idcategorynode = models.ForeignKey(Categorynode, models.DO_NOTHING, db_column='CategoryNode_idCategoryNode') # Field name made lowercase.
categorynode_idancestorcategorynode = models.ForeignKey(Categorynode, models.DO_NOTHING, db_column='CategoryNode_idAncestorCategoryNode', related_name='+') # Field name made lowercase.
class Meta:
managed = False
db_table = 'categorytree'
unique_together = (('categorynode_idcategorynode', 'categorynode_idancestorcategorynode'),)
请注意,它没有主键,应该没有主键,但是Django似乎认为会有主键关键.id字段。
Note that it does not have a Primary Key and it shouldn't have, but Django seems to think there would be an Primary Key .id Field.
推荐答案
引用 Django文档:
每个模型只需要一个字段具有 primary_key = True (显式声明或自动添加)。
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
如果您需要不使用主键访问表,则需要使用其他工具,例如 SQLAlchemy 。
If you need to access the table without primary key, you need to employ some other tool, like SQLAlchemy.
这篇关于Django错误:““字段列表”中的未知列“ categorytree.id””的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!