Django South - turning a null=True field into a null=False field
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
精彩评论