3

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.

Community
  • 1
  • 1
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

4 Answers4

9

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

John
  • 5,166
  • 27
  • 31
  • 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 Kay Aug 01 '11 at 00: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 01 '11 at 04: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 01 '11 at 10:03
1

No.

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.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 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 Kay Jul 31 '11 at 23:17
0

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/

rolling stone
  • 12,668
  • 9
  • 45
  • 63
0

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.

AAA
  • 1,962
  • 4
  • 30
  • 55