Search code examples
djangodjango-south

South Data migration in django after modifing the model


I have a project with existing class model as

class Disability(models.Model):
    child = models.ForeignKey(Child,verbose_name=_("Child"))

But with the recent architecture change i have to modify it as

class Disability(models.Model):
    child = models.ManyToManyField(Child,verbose_name=_("Child"))

now for this new change .. ( even i have to modify the existing database for the new one ) i guess data migration is the best way to do it rather than doing it manually .

i refered this online doc

http://south.readthedocs.org/en/latest/commands.html#commands-datamigration

but it has very less about data migration . and more about schema migration .

question 1 . if i do the schema migration will this make me loose all me previous data belonging to that old model.

question 2 . Even i am tring for schema migartion it is asking this ...

 (rats)rats@Inspiron:~/profoundis/kenyakids$ ./manage.py schemamigration web --auto 
  ? The field 'Disability.child' does not have a default specified, yet is NOT NULL.
  ? Since you are removing this field, you MUST specify a default
  ? value to use for existing rows. Would you like to:
  ?  1. Quit now, and add a default to the field in models.py
  ?  2. Specify a one-off value to use for existing columns now
  ?  3. Disable the backwards migration by raising an exception.
  ? Please select a choice: 1

Can anyone Explain the concept and difference between schema and data migration and how this can be achieved separately .


Solution

    1. South will try to do a transaction by moving your table data to a temporary table (I could be wrong there), then restructure the table and try to add in the origin data to the new strucutre. Like this:

    old_table -> clone -> tmp_table

    old_table ->restructure

    tmp_table.data -> table

    South will look at the field types. If there is big changes it will ask what to do. For example chaning a text field to a int field would be very hard to convert :)

    1. When you remove fields you may still want to be able to convert back to an old structure, so south will need some default data to be able to create a table with the old structure.

    Moving data is always an issue since you may change table structure and field type. For example how would you manually deal with data from a Char(max_length=100) to a Char(max_length=50)?

    Best suggestion is to keep good backups. Also take advantage of djangos fixtures. You can save fixtures for different datastructures along with south migrations.

    South will load initial_data files in the same way as syncdb, but it loads them at the end of every successful migration process

    http://south.readthedocs.org/en/latest/commands.html#initial-data-and-post-syncdb