Django Models (1054, “Unknown column in 'field list'”)

后端 未结 13 1643
庸人自扰
庸人自扰 2020-12-08 13:18

No idea why this error is popping up. Here are the models I created -

from django.db import models
from django.contrib.auth.models import User

class Shows(m         


        
相关标签:
13条回答
  • 2020-12-08 13:35

    In the appropriate field explicitly set

    primary_key = True
    
    0 讨论(0)
  • 2020-12-08 13:37

    I received this error while trying to use Django Rest Framework serializers. Make sure if you have a serializer that subclasses ModelSerializer, that you migrate any changes to the Models before writing your serializer classes (or just comment anything related to the serializer, migrate, then uncomment).

    0 讨论(0)
  • 2020-12-08 13:39

    I faced the same error like you posted above with MySQL database back-end, after long time of resolving this error, I ended up with below solution.

    1. Manually added column to database through workbench with name exactly the same as it is shown in your error message.

    2. After that run below command

    python manage.py makemigrations
    

    Now migrations will be successfully created

    1. After that run below command
    python manage.py migrate --fake
    

    Now every thing works fine without any data lose issue

    0 讨论(0)
  • 2020-12-08 13:41

    I created a model file for my app and then did several sqlall as I refined my tables. One of the changes I made was I set primary_key=True to one of my fields. At the end called for syncdb. Added a dummy value and and tried to access it with User.objects.all(), User being my model class. Although this worked fine, this error came up while printing the list returned by the all() call. It read DatabaseError: (1054, "Unknown column 'polls_user.id' in 'field list'")

    Surprisingly, I tried and could get it resolved with another call to syncdb. I remember not having seen the id column in the table at any time throughout the exercise when I checked it through the mysql command line client.

    0 讨论(0)
  • 2020-12-08 13:45

    maybe your tables schema has been changed? Also, running syncdb does not update already created tables.

    You might need to drop all the tables & then run syncdb again. Also remember to take backup of your data!!

    0 讨论(0)
  • 2020-12-08 13:46

    This is quite an old question but you might find the below useful anyway: Check the database, you're most likely missing the show_id in the appname_usershow table.

    This could occur when you change or add a field but you forget running migration.

    OR

    When you're accessing a non managed table and you're trying to add a ForeignKey to your model. Following the example above:

    class UserShow(models.Model):
        # some attributes ...
        show = models.ForeignKey(Show, models.DO_NOTHING)
    
        class Meta:
            managed = False
            db_table = 'appname_departments'
    
    0 讨论(0)
提交回复
热议问题