开发者

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

开发者 https://www.devze.com 2023-03-31 22:39 出处:网络
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 colu

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开发者_Python百科 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消