简体   繁体   English

“字段列表”中的未知列“ModelName.id””

[英]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 ),然后:

  1. Create migrations for the model and 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
  2. Add AutoField id to your model 将自动AutoField id到模型中
  3. Run 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.

相关问题 :“字段列表”中的未知列 - : Unknown column in 'field list' “字段列表”中的未知列 - Unknown Column in "field list" 1054,django 中的“字段列表”中的“未知列 'work_waterlevel.id'” - 1054, "Unknown column 'work_waterlevel.id' in 'field list'" in django “字段列表”中的未知列“”。 姜戈 - Unknown column '' in 'field list'. Django 列出新列表项时“字段列表”中的未知列“site_id” - Unknown column 'site_id' in 'field list' when listing new list items MySQL使用Django的自动ID给出“字段列表中的未知列'user.id'”错误 - MySQL gives an “Unknown column 'user.id' in 'field list'” error using Django's automatic id 1054““字段列表”中的未知列“ user_account_id_fk_id”” - 1054, “Unknown column 'user_account_id_fk_id' in 'field list'” django // 1054,“字段列表”中的未知列“rank.post_id_id”” - django// 1054, Unknown column 'rank.post_id_id' in 'field list'" OperationalError:(1054,““字段列表”中的未知列“ snippets_snippet.owner_id””) - OperationalError : (1054, “Unknown column 'snippets_snippet.owner_id' in 'field list'”) sqlalchemy.exc.OperationalError:(MySQLdb._exceptions.OperationalError)(1054,“'字段列表'中的未知列'detail.id'”) - sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1054, "Unknown column 'detail.id' in 'field list'")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM