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

Viewed 90513

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(models.Model):
    showid= models.CharField(max_length=10, unique=True, db_index=True)
    name  = models.CharField(max_length=256, db_index=True)
    aka   = models.CharField(max_length=256, db_index=True)
    score = models.FloatField()

class UserShow(models.Model):
    user  = models.ForeignKey(User)
    show  = models.ForeignKey(Shows)

Here is the view from which I access these models -

from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User

def user_page(request, username):
    try:
        user = User.objects.get(username=username)
    except:
        raise Http404('Requested user not found.')

    shows     = user.usershow_set.all()
    template  = get_template('user_page.html')
    variables = Context({
        'username': username,
        'shows'   : shows})
    output = template.render(variables)
    return HttpResponse(output)

At this point I get an error -

OperationalError: (1054, "Unknown column 'appname_usershow.show_id' in 'field list'")

As you see this column is not even present in my models? Why this error?

13 Answers

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

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'

This is a late response but hopefully someone will find this useful.

I was working on Django REST APIs using DB models. When I added a new column to my existing model(said column already existed in the db table from the start), I received the error shown below: "Unknown column ',column name>' in 'field list'"executed".

What I missed was migrating the model over to the database.

So I executed the following commands from python terminal:

  1. py -3 manage.py makemigrations ---> It will not allow NULL values for the new column added to the model, even though the column is present in the database from the start. So you need to add a default value, mine was an Integerfield, so I updated my column definition in the model as IntegerField(default=0). From here onwards it should be straightforward, if there are no other errors.

  2. py -3 manage.py migrate

  3. py -3 manage.py runserver

After executing these steps when I called my REST APIs they were working properly.

PS F:\WebApp> python manage.py makemigrations You are trying to add a non-nullable field 'price' to destination without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py Select an option: 2 PS F:\WebApp> python manage.py sqlmigrate travello 0001

BEGIN;

-- Create model Destination

CREATE TABLE travello_destination (id integer AUTO_INCREMENT NOT NULL PRIMARY KEY, name varchar(100) NOT NULL, img varchar(100) NOT NULL, desc longtext NOT NULL, offer bool NOT NULL); COMMIT; PS F:\WebApp> python manage.py makemigrations Migrations for 'travello': travello\migrations\0002_destination_price.py - Add field price to destination PS F:\WebApp> python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, travello Running migrations: Applying travello.0002_destination_price... OK

I had this issue when using a composite Primary Key of several VarChar fields and trying to call a table_set.all() queryset. Django wanted a table_name_id PK column for this queryset, there wasn't one so it threw out this error. I fixed it by manually creating the table_name_id and setting it to an auto-incremented, integer PK column instead of the composite PK. I then specified those VarChar composite columns as unique_together in the Model's meta section so they act like a PK.

Related