Search code examples
djangodjango-south

Pass South random unique default values when migrating


I am trying to forward migrate a model with existing data. The model has a new field with constraints unique=True and null=False. When I do

./manage.py schemamigration myapp --auto

South lets me specify a default value for the new field by asking:

Specify a one-off value to use for existing columns now

Usually I set this to None but since this field needs to be unique I was wondering if it is possible to pass South a unique value via:

 >>> import uuid; uuid.uuid1().hex[0:35]

This gives me an error message

! Invalid input: invalid syntax 

Any ideas if it is possible to pass South random unique default values when migrating via the commandline?

Thanks.


Solution

  • Unfortunately only the datetime module is available for use as a one-off value in a schemamigration.

    However, you can achieve the same effect by splitting this up into three migrations:

    • add new field to the model without constraints (with null=True, unique=False)
    • use a datamigration to add the UUID to the new field
    • add the constraint on the new field (with null=False, unique=True)

    Tutorial on data migrations: http://south.readthedocs.org/en/0.7.6/tutorial/part3.html#data-migrations