Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!


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.


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)

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.