Wednesday, November 20, 2013

How to start using South database migration tool in your Django project

If you are working with Django application using database, at some point you may need change your database schema. In my projects, when working with extreme style, the database changes are frequent.

The 'syncdb' handles new tables, but it does not update changes to the existing tables. One solution is to start using migration tool 'South'.

A lot of documentation for the South exists, but for some reason I was not able to get it up and running as easily as I thought I would. So I wrote the steps you have to take when starting to use South. I hope it helps.

How to start using South in your Django project

1. start using south by setting it to
    - add 'south' to INSTALLED_APPS in your settings.py
2. create needed databases, if you are starting from the scratch
    - python manage.py syncdb
3. create initial migration files
    - python manage.py schemamigration APPNAME --initial
4. make the first migration, use parameter --fake to avoid "table already exists error"
    - python manage.py migrate APPNAME --fake
5. create migration file for fixtures (if you have any)
    - manage.py datamigration APPNAME load_fixtures
6. edit the fixture file "000x_load_fixtures.py", search the file from "migrations" folder
    - edit the function forwards() to include json fixture of your own
        def forwards(self, orm):
       from django.core.management import call_command
       call_command("loaddata", "my_initial_data.json")

7. run the migration to import the fixtures
    - python manage.py migrate APPNAME



   
Links:
http://south.readthedocs.org/en/latest/index.html
http://south.readthedocs.org/en/latest/installation.html#installation
http://stackoverflow.com/questions/5687627/django-south-error-with-initial-migration
http://south.readthedocs.org/en/latest/fixtures.html

No comments: