5👍
syncdb
doesn’t change existing tables in your database, so if you run that, and then change your model, your model is now out of sync with the table it represents. Running syncdb
again will not fix that.
You either need to use something like south to do a migration, delete the table from your DB so that syncdb
will recreate it, or manually run an ALTER TABLE on your DB.
EDIT (greater detail)
When you create a subclass of Model
in models.py, it acts as a representation of a database table, but doesn’t automatically have a database table. You get that by running python manage.py syncdb
. Django, then, looks through all your models.py files, generates the SQL required to actually create a table like that and then runs it on your database. The end result is that you end up with actual database tables that are tied to your models.
However, syncdb
only creates tables. It does not alter them. So, if you go and change one of your models (add a field, change the name of a field, etc.), nothing has happened at the database level. Running syncdb
again will not help, either, because there’s no new tables to create. You have to somehow get the table to match the model and vice versa, though, so that’s where your options come in:
-
Use South (link above). South enables you to create migrations, so when you change something on your models you can run:
python manage.py schemamigration --auto yourapp
And it will generate code that will alter the table to match your model. You then need only apply the migration with:
python manage.py migrate yourapp
And you’re done. The table now matches your model and all is right in the world again.
-
You can manually delete the table from your database. You wouldn’t want to do this in production because all the data in that table will go along with it, but in development it shouldn’t be a problem. After the table is gone, you can then run:
python manage.py syncdb
Because, the table no longer exists, Django will create it, but it will create it based on your current model’s state. The net result is the same, your model and table match, so you’re good to go.
-
You can manually alter the table. This requires that you figure out what SQL needs to be applied to change the table to match your model. You run this SQL on your database, and then the table is in parity with the model.
The point is that somehow, someway, you must update the table to reflect any changes you make to your models. The model isn’t the table, it’s just a programmatic representation of it.
1👍
The column might not necesarrily exist. The sql command just shows the sql used to create it, It is based on your current model. You could delete the table and re syncdb or manually add the column. https://docs.djangoproject.com/en/dev/ref/django-admin/#sql-appname-appname
I think there is a little confusion regarding the django Model and the actual database table
The django model is just some python code. It is a python object that is connected to a sql table. The database backend is specified in settings.py. The database contains the actual table. The error you are encountering is indicating that the python model is not the same as the actual database table.
- [Django]-Prefetch_related() join all on first item
- [Django]-Exception while installing packages in Python
- [Django]-Django: The value of the id field is coming None