While developing an application with Django, it may take some time for the database model to stabilize. It’s common that you’ll want to add new columns to existing tables. Unfortunately, the ./manage.py syncdb command will not be sufficient in this case. If the database doesn’t contain any important data, a simple solution is to remove the existing database and create it again. But Django’s ./manage doesn’t directly include a command to remove the database. However, this is fairly simple to do by chaining two commands like so:
./manage.py sqlclear appname | ./manage.py dbshell
You’ll have to substitute “appname” with the name of your Django app, of course. The first command prints the sequence of SQL commands necessary to drop all tables for the given app. You then just have to pipe these SQL commands to Django’s database shell.
Then you can create the database again with the usual:
./manage.py syncdb
I like to program iteratively, and to increasingly add layers of complexity to my program, so having this sort of loop in place is very important for me.
Hi Telmo!
Great to see that you are a pythonista now! :) Django rocks and I use it a lot!
As a suggestion to this problem… take a look at South Migrations!
http://south.aeracode.org/
I use this in all my projects and its really great and you can add and remove fields to your models and only have to do:
./manage.py schemamigration app_name --auto # to create migration
./manage.py migrate app_name # to apply migration!
its great to update remote versions with new features!
Regards! See you one of these days!
Pedro
Hey Pedro,
Nice to hear from you, and thanks for your tip! Yeah, Python is my favorite language these days. I love it for its simplicity and for the “batteries included”.
These days I’ve been playing with Flask (http://flask.pocoo.org/) and MongoDB. Django is really cool, but I’m doing some weird stuff and it gets in the way a bit.
See you!
Telmo.