开发者

Django South - turning a null=True field into a null=False field

开发者 https://www.devze.com 2023-02-06 14:00 出处:网络
My question is, what is the best practice for turning a null=True field into a null=False field using Django S开发者_Python百科outh. Specifically, I\'m working with a ForeignKey.You should write first

My question is, what is the best practice for turning a null=True field into a null=False field using Django S开发者_Python百科outh. Specifically, I'm working with a ForeignKey.


You should write first a data migration: http://south.aeracode.org/docs/tutorial/part3.html and then make the schemamigration.


If you want to turn nullable ForeignKey into non-nullable one, then it can be problematic if you have any rows with NULL for that field (column). In such case you need to either remove or fix them - possibly with custom data migration, like diegueus9 mentioned in the other answer.

But if you don't have any rows with NULL in that column, e.g. because you put that null=True only in case you might need it in the future, then you should be able to do a simple automatic schema migration:

$ ./manage.py schemamigration myapp remove_null_from_fkey --auto
(...)
$ ./manage.py migrate myapp
0

精彩评论

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