Take the 2-minute tour ×

Possible Duplicate:
update django database to reflect changes in existing models

I've used Django in the past and one of the frustrations I've had with it as an ORM tools is the inability to update an existing database with changes in the model. (Hibernate does this very well and makes things really easy for updating and heavily modifying a model and applying this to an existing database.) Is there a way to do this without wiping the database every time? It gets really old having to regenerate admin users and sites after every change in the model which I'd like to play with.

share|improve this question

marked as duplicate by Ignacio Vazquez-Abrams, Daniel DiPaolo, Daniel Roseman, Ninefingers, tvanfosson Jan 26 '12 at 13:05


4 Answers 4

up vote 8 down vote accepted

You will want to look into South. It provides a migrations system to migrate both schema changes as well as data from one version to the next.

It's quite powerful and the vast majority of changes can be handled simple by going

manage.py schemamigration --auto
manage.py migrate

The auto functionality does have it limits, and especially if the change is going to be run on a production system eventually you should check the code --auto generated to be sure it's doing what you expect.

South has a great guide to getting started and is well documented. You can find it at http://south.aeracode.org

share|improve this answer
Excellent. Yeah, the main point is for development, but this will come in handy in deployment too, going from one version of the application to the next. Is it available in the Python general repo (ie for Buildout)? –  Naftuli Tzvi Kay Aug 1 '11 at 0:16
I assume you mean PyPy. And yes you can use pip or easy_install to install it. Yeah it's actually fantastic for deployment you just need to be aware of the gotcha's of --auto for those cases. In particular that auto can't tell the difference between renaming a field and deleting one field and adding another. So you need to be careful and look at the generated code to make sure it makes sense –  John Aug 1 '11 at 4:33
pip install south / easy_install south. to note, you can also use it for the live env. its bulletproof migration system ensures data integrity. I think the best feature is the migration can be versioned. upon each migration a py file is created. when you commit it to git or svn, you can roll back the database if it goes wrong. –  Glycerine Aug 1 '11 at 10:03
South is an epic win. –  Naftuli Tzvi Kay Aug 3 '11 at 15:27


As the documentation of syncdb command states:

Syncdb will not alter existing tables

syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.

share|improve this answer
Alright. Well, is there any way (even through a third-party product) to do this or to at the very least backup, drop, regenerate, and restore the data to make things easier? –  Naftuli Tzvi Kay Jul 31 '11 at 23:17

South seems to be how most people solve this problem, but a really quick and easy way to do this is to change the db directly through your database's interactive shell. Just launch your db shell (usually just dbshell) and manually alter, add, drop the fields and tables you need changed using your db syntax.

You may want to run manage.py sqlall appname to see the sql statements Django would run if it was creating the updated table, and then use those to alter the database tables and fields as required.

The Making Changes to a Database Schema section of the Django book has a few examples of how to do this: http://www.djangobook.com/en/1.0/chapter05/

share|improve this answer

I manually go into the database - whatever that may be for you: MySQL, PostgreSQL, etc. - to change database info, and then I adjust the models.py accordingly for reference. I know there is Django South, but I didn't want to bother with using another 3rd party application.

share|improve this answer
