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

2019-03-16 19:16发布

My question is, what is the best practice for turning a null=True field into a null=False field using Django South. Specifically, I'm working with a ForeignKey.

2条回答
可以哭但决不认输i
2楼-- · 2019-03-16 19:41

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
查看更多
混吃等死
3楼-- · 2019-03-16 19:44

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

查看更多
登录 后发表回答