33

I have made a model change from

standard = models.ManyToManyField(Standard)

to

standard = models.ManyToManyField(Standard, blank=True, null=True)

South schemamigration for this app doesn't recognize the change?

Similar to this question, which is unanswered: South migrations and changes to many-to-may fields

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
npradeetw
  • 358
  • 1
  • 3
  • 7
  • What exactly does South say when you try to run `./manage.py schemamigration yourapp --auto`? – Daniel Rosenthal Aug 14 '13 at 22:01
  • Only thing I can think of is that you aren't saving `models.py`, but I highly doubt it. Sorry I can't help. – Daniel Rosenthal Aug 14 '13 at 22:17
  • Just a thought: should south do something? A ManyToManyField will create an intermediate table, so its either you have a record in that table or not. I mean both fk columns should still be mandatory on the intermediate table. Sorry if I got this wrong! – ppetrid Aug 14 '13 at 23:09

1 Answers1

62

That behavior is correct: null doesn't mean anything at the database level when used with a ManyToManyField. The declaration of a ManyToManyField causes the creation of an intermediate table to hold the relationship, and although Django will create a standard attribute on your model instance for your convenience, there is no actual column representing it that could be nulled. By definition there can always be zero instances of the relationship.

blank=False, though, does have an effect on validation (when using model forms like the admin app, for example), forcing the user to choose at least one relation.

(Note that Django's built-in migration system creates migrations for just about any change to a model, regardless of whether it affects the database or not. So this change could lead to a migration, but it wouldn't affect the database or whether or not you could have zero instances of the relationship.)

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102