views:

1639

answers:

4

Hello, I have recently updated my model, added a BooleanField to it however when I do python manage.py syncdb, it doesn't add the new field to the database for the model. How can I fix this ?

+1  A: 

Havent used django in a while, but i seem to remember that syncdb does perform alter commands on db tables. you have to drop the table then run again and it will create again.

edit: sorry does NOT perform alter.

Aliixx
Then it's not running `alter table` commands, it's running `create table` commands.
Dominic Rodger
well I have data in the db so it would be great if I overcome this problem without dropping.
Hellnar
I think you wanted to say, that syncdb doesNOT perform alter commands. You do not have to drop the table, you can manually add the new field to your sql too.
Zayatzz
yes but the question was referencing the use of syncdb, so in this case you would have to drop the table to use syncdb or use the plugin mentioned below.
Aliixx
+11  A: 

Django doesn't support migrations out of the box. There is a pluggable app for Django that does exactly that though, and it works great. It's called South.

If you have difficulty setting it up edit your question and I'll gladly help you out (you might want to post a comment on my answer so I get notified).

Dominic Rodger
+1 South is the awesome!
T. Stone
+1 South works great. Have used it in production.
codeape
thanks again Dominic!
Hellnar
A: 

Django currently does not do this automatically. Your options are:

  1. Drop the table from the database, then recreate it in new form using syncdb.
  2. Print out SQL for the database using python manage.py sql (appname), find the added line for the field and add it manually using alter table SQL command. (This will also allow you to choose values of the field for your current records.)
  3. Use South (per Dominic's answer).
che
+1  A: 

Follow these steps:

  1. Export your data to a fixture using the dumpdata management command
  2. Drop the table
  3. Run syncdb
  4. Reload your data from the fixture using the loaddata management command
Soviut
A complete drop and reload for something that should be done in-place? That's terrible.
Glenn Maynard
Its a simple management command, not a migration suite! Django has no way of predicting how your data has changed or how to preserve it so it insists that you do it yourself. If you don't like it, use a migration tool like South.
Soviut