[Django]-Is there a way to update the database with the changes in my models?

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

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

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/

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

Leave a comment