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

后端 未结 13 1645
庸人自扰
庸人自扰 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:49

    Normally I get this when when I'm trying to access field which doesn't exist in Database.

    Check if the field exist in the database. If you change model and perform syncdb it won't update the database, I'm not sure if that's the case.

    On other note Django offers shortcut to replace try/except block in your code using get_object_or_404. (available in django.shortcuts )

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

    can be changed to:

    user = get_object_or_404(User, username=username)
    
    0 讨论(0)
  • 2020-12-08 13:50

    I have met the same problems:

    First, run

    manage.py sqlall [appname]
    

    and you can find:

    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    

    and I add the column manual:

    ALTER TABLE tb_realtime_data ADD id integer AUTO_INCREMENT NOT NULL PRIMARY KEY FIRST;
    

    and then it worked.

    I think django will add the column called id itself.

    For convenience, each model is given an autoincrementing primary key field named id unless you explicitly specify primary_key=True on a field (see the section titled “AutoField” in Appendix A).

    you can click here for details.

    Good luck.

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

    The direct solution is delete files under folder ../Project/App/migrations, and the method to avoid this problem is create new databaes table column instead of chagne the existing one.

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

    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

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

    As @inception said my tables schema had changed & running syncdb did not update already created tables.

    Apparently any changes to the models when updated through syncdb does not change (as in update/modify) the actual tables. So I dropped the relevant DB & ran syncdb on empty DB. Now it works fine. :)

    For others, SOUTH data migration tool for Django seems to be favorite option. It seems to provide options which django models & syncdb falls short on. Must check out...

    Update 29th Sept 2019: From Django 1.7 upwards, migrations are built into the core of Django. If you are running a previous lower version of Django, you can find the repository on BitBucket.

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

    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.

    0 讨论(0)
提交回复
热议问题