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

别来无恙 提交于 2019-12-03 13:36:25

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/4761048/django-south-turning-a-null-true-field-into-a-null-false-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!