Search code examples
pythondjangomigrationdjango-south

Django South - Creating initial migration for an app that has already populated tables


Scenario: I have an app in my Django application that I have never put under South management. I ran a syncdb a long time ago, and the models of this app have never had to change. Throughout time, I have obviously added data to those tables.

Now, I wish to put this app under south management, but once the tables already exist, I can create the migration file, but naturally, I cant execute them. I get a database error for existing tables:

django.db.utils.DatabaseError: (1050, "Table 'ooyala_ooyalaitem' already exists")

This is pretty obvious for me. What I was wondering is if there is a smart way to run the migrate command to use the current tables. I did not want to dump the data, delete tables manually, run the migration and repopulate things, and neither I wanted to create a data migration for this.

Any ideas on this? Is it even possible?

Thanks for your time.


Solution

  • This is covered in the manual.

    Converting an app to use South is very easy:

    • Edit your settings.py and put ‘south’ into INSTALLED_APPS (assuming you’ve installed it to the right place)
    • Run ./manage.py syncdb to load the South table into the database. Note that syncdb looks different now - South modifies it.
    • Run ./manage.py convert_to_south myapp - South will automatically make and pretend to apply your first migration.

    Note that you’ll need to convert before you make any changes; South detects changes by comparing against the frozen state of the last migration, so it cannot detect changes from before you converted to using South.