views:

903

answers:

6

I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?

+2  A: 

deseb is a great tool for that.

Having it installed, you can write ./manage.py sqlevolve and it'll generate sql commands necessary to keep the database structure in sync with your models.

Antony Hatchkins
This looks like what I needed. Does sqlevolve commit the changes as well?
theactiveactor
+6  A: 

Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.


note: syncdb can't update your existing tables. Sometimes it's impossible to decide what to do automagicly - that's why south scripts are this great.

kender
thanks for pointing out this interesting tool. For anyone interested, there's a insightful discussion about django database migration here:http://code.djangoproject.com/wiki/SchemaEvolution
theactiveactor
+1  A: 

You need to drop your tables before you can recreate them with syncdb.

If you want to preserve your existing data, then you need to unload your database, drop your tables, run syncdb to build a new database, then reload your old data into your new tables.

There are tools that help with this. However, in many cases, it's just as easy to do it manually.

S.Lott
+1  A: 

Django's syncdb doesn't alter existing tables in the database so you have to do it manually. The way I always do it is:

  1. Change the model class first.
  2. Then run: manage.py sql myapp.
  3. Look at the sql it prints out and see how it represented the change you are going to make.
  4. Make the change manually using your database manager.
  5. Check to see if everything worked correctly using the admin site.

If you are using sqllite a good manager is the firefox plugin: link

Juan Besa
+4  A: 

Another tool would be django evolution. No table dropping needed in most cases.

django evolution

Just install it as any other django app and run:

python manage.py evolve --hint --execute

Tom Tom
+8  A: 

Another option, not requiring additional apps, is to run:

python manage.py reset <your_app>

This will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:

python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json

If your changes break your old schema this won't work - in which case tools like south or django evolution are great.

zlovelady
Thanks for this. Just what I was looking for.
Rob Flaherty
I think this solution is best.
xiao
thanks, the best solution :)
Uku Loskit