gpt4 book ai didi

Django South 迁移错误 "contains null values"字段为 null=True

转载 作者:行者123 更新时间:2023-12-03 07:10:14 25 4
gpt4 key购买 nike

向模型添加简单缩略图字段时

easy_thumbnail = ThumbnailerImageField(
null=True, blank=True, verbose_name=_("Easy_Thumbnails"),
upload_to="easy_thumbnails",
resize_source=dict(size=(100, 100), crop="smart"),
)

执行./manage.py schemamigration test --auto时,South 会生成以下迁移:

def forwards(self, orm):
# Adding field 'Test.easy_thumbnail'
db.add_column('test_test', 'easy_thumbnail', self.gf('django.db.models.fields.files.ImageField')(), keep_default=False)
[...]
models = {
'test.test': {
'Meta': {'object_name': 'Test'},
'easy_thumbnail': ('django.db.models.fields.files.ImageField', [], {}),

执行./manage.py migrate test时,出现此错误:

The error is `django.db.utils.IntegrityError: column "easy_thumbnail" contains null values`

最佳答案

修改迁移脚本如下即可解决问题:

  • 在 db.add_column 中添加“null=True, Blank=True”
  • 在模型['test.test']['easy_thumbnail']中添加“'null': 'True', 'blank': 'True'”

代码:

def forwards(self, orm):
# Adding field 'Test.easy_thumbnail'
db.add_column('test_test', 'easy_thumbnail', self.gf('django.db.models.fields.files.ImageField')(null=True, blank=True), keep_default=False)
[...]
models = {
'test.test': {
'Meta': {'object_name': 'Test'},
'easy_thumbnail': ('django.db.models.fields.files.ImageField', [], {'null': 'True', 'blank': 'True'}),

关于Django South 迁移错误 "contains null values"字段为 null=True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com