Search code examples
pythondjangodjango-modelsdjango-south

South: run a migration for a column that is both unique and not null


Using South/Django, I am running into a problem where I'm trying to add a UNIQUE and NOT NULL column for a model with existing rows in the database. South prompts me to specify a default for the column, since it is NOT NULL. But since it also has a UNIQUE constraint, I can't add a default to the field in models.py, nor can I specify a one-off value because it'll be the same on all the rows.

The only way I can think of to get around this is to create a nullable column first, apply the migration, run a script to populate the existing rows with unique values in that column, and then add another migration to add the UNIQUE constraint to that column.

But is there a better way of accomplishing the same thing?


Solution

  • Yes, this is the approach you should take. You should be doing schemamigration -> datamigration -> schemamigration for this. unfortunately if there is no way to do it in SQL, south cannot do it either.