Django 南方:为什么是;列<;colname>;“包含空值”;提供有效的一次性默认值时出错
最初,项类的模型属性位置定义如下:Django 南方:为什么是;列<;colname>;“包含空值”;提供有效的一次性默认值时出错,django,django-south,Django,Django South,最初,项类的模型属性位置定义如下: location = models.ForeignKey('Location', related_name='+', null=True, on_delete=models.SET_NULL) 然后重新定义为: location = models.ForeignKey('Location', related_name='+', on_delete=models.PROTECT) 因为定义上的改变,我执行了South的计划。南韩对此作出了回应 字段“Item
location = models.ForeignKey('Location', related_name='+', null=True, on_delete=models.SET_NULL)
然后重新定义为:
location = models.ForeignKey('Location', related_name='+', on_delete=models.PROTECT)
因为定义上的改变,我执行了South的计划。南韩对此作出了回应
字段“Item.location”未指定默认值,但为
不为空。由于要使此字段不可为空,因此必须
指定用于现有行的默认值
我选择了选项“2”,并提供了现有位置的主键(整数)
但是当我运行migrate时,我得到了以下错误:
django.db.utils.IntegrityError:列“location\u id”包含空值
当我提供了一个有效的默认位置PK时,我不明白为什么会出现这个错误。这真是令人难以置信。请帮忙~谢谢
迁移规范:
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'})
}
}
这个问题似乎是由引起的,实际上解决方案已经发布,但是您可能只想在相关迁移中添加一行
...
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')())
...
...
迁移规范看起来像什么?@DmitryBeransky-我已经在我的帖子中添加了迁移规范。请让我知道,如果你能看到任何可能导致问题。我也有同样的问题。您找到解决方案了吗?@Intenex-我假设您出现此错误是因为您将某列“
my_col
”从null=True
更改为null=False
。如果是这种情况,一种快速的方法是用空字符串填充my_col
为NULL的行。完成此操作后,您应该能够执行migrate
,而不会出现错误。我通过手动更改每个空条目来修复此问题,如下所示: