Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 823
Jamie Forrest Avatar asked Aug 29 '11 04:08

Jamie Forrest


1 Answers

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.

like image 172
Thomas Avatar answered Oct 29 '22 12:10

Thomas