[英]Unknown column 'ModelName.id' in 'field list'"
I am using Django with mysql. I have created models.py
using command inspectdb
from existing database in mysql. It has a model as below:我正在使用 Django 和 mysql。我使用 mysql 中现有数据库中的命令inspectdb
创建了models.py
。它有一个 model,如下所示:
class Participationdata(models.Model):
userid = models.ForeignKey('Volunteer', models.DO_NOTHING, db_column='UserId') # Field name made lowercase.
eventid = models.ForeignKey('Events', models.DO_NOTHING, db_column='EventId') # Field name made lowercase.
ptrflag = models.IntegerField(db_column='PtrFlag', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'ParticipationData'
unique_together = (('userid', 'eventid'),)
I have inserted the data into database using this model successfully.我已成功使用此 model 将数据插入数据库。 But while selecting data it is showing the error like:但是在选择数据时显示如下错误:
django.db.utils.OperationalError: (1054, "Unknown column 'Participationdata.id' in 'field list'")
How should I resolve this?我应该如何解决这个问题?
您在Participationdate
表中是否有id
字段?
Usually you would set primary_key = True
on your eventid
field, so Django uses it as a default id
. 通常,您将在eventid
字段上设置primary_key = True
,因此Django将其用作默认id
。 Look at the answer in Custom id field in Django model 查看Django模型中“ 自定义ID”字段中的答案
But it is a ForeignKeyField
, so you have to add id column to your database and model. 但这是一个ForeignKeyField
,因此您必须向数据库和模型添加id列。 You can remove managed = False
(default is True
) and then: 您可以删除managed = False
(默认为True
),然后:
migrate --fake-initial
(since you have table already, see more: https://docs.djangoproject.com/en/1.10/ref/django-admin/#cmdoption-migrate-fake-initial ) 为模型创建迁移并migrate --fake-initial
(因为您已经有表,请参见更多信息: https : migrate --fake-initial
) AutoField id
to your model 将自动AutoField id
到模型中 makemigrations
and migrate
as usual. 运行makemigrations
并照常migrate
。 That way django uses your already-existing table, but you can modify it via models. 这样,django使用您已经存在的表,但是您可以通过模型对其进行修改。
I solve this error when I add primary_key=True
to my id, because Django need a primary key.当我将primary_key=True
添加到我的 id 时,我解决了这个错误,因为 Django 需要一个主键。 In my code, I add the primary key in my View id, because the inspectdb
don't get the key from the database view在我的代码中,我在视图 ID 中添加了主键,因为inspectdb
没有从数据库视图中获取键
class ViewRespuestaEncuestas(models.Model):
id_encuesta = models.IntegerField(primary_key=True)
encuesta = models.CharField(max_length=45, db_collation='utf8_general_ci')
id_usuario = models.IntegerField()
nombre_usuario = models.CharField(
max_length=40, db_collation='utf8_general_ci', blank=True, null=True)
id_pregunta = models.IntegerField()
pregunta = models.TextField(db_collation='utf8_general_ci')
id_respuesta = models.IntegerField()
respuesta = models.CharField(max_length=60, db_collation='utf8_general_ci')
valor = models.IntegerField(blank=True, null=True)
class Meta:
managed = False # Created from a view. Don't remove.
db_table = 'view_respuesta_encuestas'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.