Originally, the model attribute location for Item class was defined like the following:
location = models.ForeignKey('Location', related_name='+', null=True, on_delete=models.SET_NULL)
It then got redefined to:
location = models.ForeignKey('Location', related_name='+', on_delete=models.PROTECT)
Because of the change in definition, I executed South’s schemamigration. South responded with
The field ‘Item.location’ does not have a default specified, yet is
NOT NULL. Since you are making this field non-nullable, you MUST
specify a default value to use for existing rows.
I picked choice ‘2’ and provided the PK (integer) of an existing Location.
But when I ran migrate, I got the following error:
django.db.utils.IntegrityError: column “location_id” contains null values
I don’t understand why I got this error when I had provided a valid default location PK. This is really mind-boggling. Please help~ Thanks.
Migration spec:
def forwards(self, orm):
# Changing field 'Item.location'
db.alter_column('lend_borrow_item', 'location_id', self.gf('django.db.models.fields.related.ForeignKey')(default=11, to=orm['app_name.Location']))
def backwards(self, orm):
# Changing field 'Item.location'
db.alter_column('lend_borrow_item', 'location_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['app_name.Location']))
models = {
'app_name.location': {
'Meta': {'ordering': "['name']", 'object_name': 'Location'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '20'})
},
'lend_borrow.item': {
'Meta': {'object_name': 'Item'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'location': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'to': "orm['app_name.Location']"}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
}
}
This problem seems to be caused by South Defect #627