[英]django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id
When I run ./manage.py migrate
,error happens django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id
.当我运行
./manage.py migrate
,会发生错误django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id
。 models.py
is models.py
是
class Area(models.Model):
name = models.CharField(max_length=20, verbose_name='area', null=True)
class User(models.Model):
name = models.CharField(max_length=200,null=True)
age = models.CharField(max_length=200,null=True)
area = models.ForeignKey('Area', default="")
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area')
class City(models.Model):
name = models.CharField(max_length=20, verbose_name='region')
prefecture = models.ForeignKey('Prefecture')
class Price(models.Model):
name = models.CharField(max_length=20, verbose_name='price')
PRICE_RANGE = (
('a', 'under500'),
('b', '500-1000'),
('c', 'upper1000'),
)
price_range = models.CharField(max_length=1, choices=PRICE_RANGE)
city = models.ForeignKey('City')
When I wrote area = models.ForeignKey('Area')
,I got an error You are trying to add a non-nullable field 'area' to transaction without a default;当我写
area = models.ForeignKey('Area')
,我得到一个错误你试图在没有默认值的情况下向事务添加一个不可为空的字段“区域”; we can't do that (the database needs something to populate existing rows).我们不能这样做(数据库需要一些东西来填充现有的行)。 Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in
models.py
.请选择一个修复:1)现在提供一次性默认值(将在所有现有行上设置此列的空值)2)退出,让我在
models.py
添加一个默认值。 What is wrong?怎么了? How can I fix this?
我怎样才能解决这个问题?
Now models.py is现在models.py是
class Area(models.Model):
name = models.CharField(max_length=20, verbose_name='area', null=True)
class User(models.Model):
name = models.CharField(max_length=200,null=True)
age = models.CharField(max_length=200,null=True)
area = models.ForeignKey('Area', default="")
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area', null=True, blank=True)
class City(models.Model):
name = models.CharField(max_length=20, verbose_name='region')
prefecture = models.ForeignKey('Prefecture', null=True, blank=True)
class Price(models.Model):
name = models.CharField(max_length=20, verbose_name='price')
PRICE_RANGE = (
('a', 'under500'),
('b', '500-1000'),
('c', 'upper1000'),
)
price_range = models.CharField(max_length=1, choices=PRICE_RANGE)
city = models.ForeignKey('City', null=True, blank=True)
I also got same error.I already tried cache.clear() .我也遇到了同样的错误。我已经尝试过 cache.clear() 。
In the model below you are trying to add area as a ForeignKey.在下面的模型中,您尝试将区域添加为外键。 Since this
Prefecture
table has some data already Django does not know what to add in area field for existing rows.由于这个
Prefecture
表已经有一些数据,Django 不知道在现有行的区域字段中添加什么。
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area') # non null-able field Django does
# not know what to add
Simple solution.简单的解决方案。 Provide a default or add null
提供默认值或添加空值
class Prefecture(models.Model):
name = models.CharField(max_length=20, verbose_name='city')
area = models.ForeignKey('Area', null=True)
Note: for all existing rows in Prefecture
now area
field will be null.注意:对于现在
Prefecture
所有现有行, area
字段将为空。 You can add this field now for new rows.您现在可以为新行添加此字段。
You'll first need to delete the previously generated migration file.您首先需要删除之前生成的迁移文件。
Otherwise, even if you fix your models.py
, you will keep getting the same error because ./manage.py migrate
will keep trying to apply the previous migration.否则,即使您修复了
models.py
,您也会不断收到相同的错误,因为./manage.py migrate
将继续尝试应用先前的迁移。
After you deleted the previously generated migration file, use the following:删除之前生成的迁移文件后,使用以下命令:
area = models.ForeignKey('Area', blank=True, null=True)
This will make the area
attribute optional, and you will be able to generate a new migration file and proceed with ./manage.py migrate
.这将使
area
属性可选,您将能够生成新的迁移文件并继续./manage.py migrate
。
After you've done that, you can optionally write a data migration script to fill in the missing area
attributes.完成此操作后,您可以选择编写数据迁移脚本来填充缺失的
area
属性。 You can read more about in in the documentation .您可以在文档中阅读更多信息。
You can also fill-in this attribute manually for your existing data, instead of writing a migration script (for instance, buy directly updating your database or by using django-admin).您还可以为现有数据手动填写此属性,而不是编写迁移脚本(例如,购买直接更新数据库或使用 django-admin)。
Once you make sure that all your data has the area
attribute filled-in, you can make your final change:确保所有数据都填充了
area
属性后,您可以进行最终更改:
area = models.ForeignKey('Area')
When you apply this migration, you will simply add a NOT NULL constraint to this field in your database, and it should be OK if you filled-in the data in the previous step.当您应用此迁移时,您只需在数据库中的此字段中添加一个 NOT NULL 约束,如果您在上一步中填写了数据,则应该可以。
如上面的答案,[null = True] 可用,我建议另一个 [default = None] 也可用
It may be possible you have not executed makemigration
and migrate
.您可能还没有执行
makemigration
和migrate
。
I faced below error:我面临以下错误:
django.db.utils.IntegrityError: NOT NULL constraint failed: adminapp_responses.created_by_id
django.db.utils.IntegrityError: NOT NULL 约束失败: adminapp_responses.created_by_id
But after makemigration
and migrate
, it is gone.但是在
makemigration
和migrate
,它就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.