ApathyBear
ApathyBear

Reputation: 9595

Schemamigration: adding a simple Null field

Okay, so I had an app called locationmanager with a model that looks like this:

class Location(models.Model):
    region = models.ForeignKey(Region)

and I want to change it to this

class Location(models.Model):
    region = models.ForeignKey(Region, blank=True, null=True)

So I ran a schemamigration and received the following:

$ python manage.py schemamigration locationmanager --auto
 ? The field 'Location.region' does not have a default specified, yet is NOT NULL.
 ? Since you are making this field nullable, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now.
 ?  2. Specify a one-off value to use for existing columns now
 ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
 ? Please select a choice: 

This is confusing to me. And yes I have read the south docs.

  1. Firstly, why is it saying 'Location.region' does not have a default specified, yet is NOT NULL. The alterations I made to the field ARE making it the case that it is null, what gives? An explanation would be great.

  2. Why do I need to specify a default? Once I specify this default where does it live? In an SQL table? Does this matter if I have existing data or is this simply for the scheme itself?

3.I sort of understand option 1,2 but what exactly would option 3 do? Why is it useful?

Thank you tremendously.

Upvotes: 0

Views: 98

Answers (1)

Matt
Matt

Reputation: 10322

Because migrations work both way. One day you may want to rollback to the state it was before and in that case you'll need a default value for the mandatory field.

Oh, and in this case I usually always choose option 2 and enter 0 as the value. Works fine.

Upvotes: 1

Related Questions