South: why the <colname> column error contains null values ​​"when a valid one-time default value was provided

Initially, the model location attribute for the Item class was defined as follows:

location = models.ForeignKey('Location', related_name='+', null=True, on_delete=models.SET_NULL) 

Then it was redefined:

 location = models.ForeignKey('Location', related_name='+', on_delete=models.PROTECT) 

Due to a change in definition, I performed the south circuit. South answered

The "Item.location" field does not specify a default value, but NOT ZERO. Since you make this field non-null, you MUST specify a default value for existing rows.

I selected option "2" and provided a PK (integer) of the existing location.

But when I migrated, I got the following error:

django.db.utils.IntegrityError: the location_id column contains null values

I do not understand why I received this error when I provided a valid default PK location. This is truly stunning. Please help ~ Thank you.

Migration specification:

 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'}) } } 
+4
source share
2 answers

This issue seems to be caused by South Defect # 627

+5
source

In fact, the solution has already been published, but you can simply add the line to the corresponding migration.

 ... class Migration(SchemaMigration): def forwards(self, orm): ... # Add the following line before the ``alter_column`` command orm['your_app.YourModel'].objects.filter( your_field__isnull=True).update(your_field=any_default_value) # This is the line that causes the exception db.alter_column(u'your_app.YourModel', 'your_field', self.gf('django.db.models.fields.Whatever')()) ... ... 
0
source

Source: https://habr.com/ru/post/1382573/


All Articles