Of Django-south migrations

Last week at work I stumbled upon this problem: two of the Django-south migrations made conflicting changes to the database schema. Let’s say 0003_migration_1 & 0003_migration_2 are the two migrations applied in the same order. But, the models in migration_2 do not include the changes made by migration_1.

This causes a problem while creating a new migration

   $ python manage.py schemamigration my_app –auto [–stdout]

creates a new migration: 0004_migration_1. While creating this new migration, Django-south does a diff(1) between the current models definition and the one in the last applied migration ie: 0003_migration_2. But since 0003_migration_2 did not include the model changes from 0003_migration_1, the new migration: 0004_migration_1 includes those changes. And when you try to apply this new migration

   $ python manage.py migrate my_app

it causes an error wherein Django-south says that some of the database changes already exist in the database.

Now one solution, though not so straightforward, is to manually merge the two migrations: 0003_migration_1 and 0003_migration_2, so that the resulting new migration script combines the two model definitions.

Thankfully, Django-south has a way to create empty migrations and freeze the models definition. Empty migrations don’t make any schema chagnes, but only store the updated model definitions.

   $ python manage.py schemamigration –freeze=my_app –empty –auto [–stdout]

creates a new migration 0004_empty_migration_1, which holds the latest model definitinons, ie changes made by 0003_migration_1 and 0003_migration_2 together into one. After applying the empty migration, I could create the new migration: 0005_migration_1 to include just the new schama changes and exclude the ones which were already applied by previous migrations.

* Tip: use –stdout option to see and verify the schema changes included in the new migration.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s