6

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.

orokusaki
  • 55,146
  • 59
  • 179
  • 257
  • Do you know what the new default value will be for the foreign key column after null=False? Rather, will it be same for all or will some special processing need to take place to determine what it will be? Will there be a default? What database are you using? – mkelley33 Jan 21 '11 at 17:23

2 Answers2

6

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

diegueus9
  • 29,351
  • 16
  • 62
  • 74
3

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
Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83