django.db.utils.IntegrityError:列“venue_city”包含空值

问题描述 投票:0回答:7

我在 stackoverflow 和 google 上阅读了很多其他帖子,但找不到解决方案。

这一切都是从我将模型从 CharField 更改为ForeignKey 时开始的。 我收到的错误是:

Operations to perform:
  Synchronize unmigrated apps: gis, staticfiles, crispy_forms, geoposition, messages
  Apply all migrations: venues, images, amenities, cities_light, registration, auth, admin, sites, sessions, contenttypes, easy_thumbnails, newsletter
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying venues.0016_auto_20160514_2141...Traceback (most recent call last):
  File "/Users/iam-tony/.envs/venuepark/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "venue_city" contains null values

我的模型如下:

class Venue(models.Model):
    venue_city = models.ForeignKey(City, null=True,)
    venue_country=models.ForeignKey(Country, null=True)

venue_country 之前不存在,因此迁移成功。但venue_city是一个CharField。

我对迁移文件进行了一些更改,以便它执行 SQL,如下所示:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('venues', '0011_venue_map_activation'),
    ]

    migrations.RunSQL(''' ALTER TABLE venues_venue ALTER venue_city TYPE integer USING  venue_city::integer '''),

    migrations.RunSQL(''' ALTER TABLE venues_venue ALTER venue_city RENAME COLUMN venue_city TO venue_city_id '''),

    migrations.RunSQL(''' ALTER TABLE venues_venue ADD CONSTRAINT venues_venus_somefk FOREIGN KEY (venue_city_id) REFERENCES  cities_light (id) DEFERRABLE INITIALLY DEFERRED'''),

提前致谢!

更新:我的新迁移文件:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('cities_light', '0006_compensate_for_0003_bytestring_bug'),
        ('venues', '0024_remove_venue_venue_city'),
    ]

    operations = [
        migrations.AddField(
            model_name='venue',
            name='venue_city',
            field=models.ForeignKey(null=True, to='cities_light.City'),
        ),
    ]
sql django postgresql django-models django-migrations
7个回答
31
投票

看起来您在创建迁移文件后添加了

null=True
。因为
venue_city
不是迁移文件中可为空的字段

请按照以下步骤操作。

1) Drop venue_city & venue_country from your local table
3) Delete all the migration files you created for these `CharField to a ForeignKey` change
4) execute `python manage.py makemigrations`
5) execute 'python manage.py migrate'

应该有效


7
投票

有类似的问题,我通过删除以前的迁移文件解决了它。没有技术解释


6
投票

我通过将

null = True
添加到导致问题的(自动生成的)迁移文件和模型中来解决它。然后再次
migrate
,失败的迁移现在将成功。当您在模型中也更改它时,
makemigration
此后将检测不到任何更改。


3
投票

请按照以下步骤操作:-

  • 在 Venue 模型类中添加 null=True, Blank=True 例如:venue_city = models.ForeignKey(City, null=True, Blank=True)
  • 从应用程序的迁移文件夹中删除venues.0016_auto_20160514_2141迁移文件
  • 然后运行 python manage.py makemigrations
  • 然后运行 python manage.py migrate

无需删除表或从迁移表中删除迁移


0
投票

我通过以下方式解决了这个问题:

  1. 首先删除遇到问题的最后一个迁移
  2. 然后添加
    venue_city = models.CharField(blank=True, null=True)
  3. 最后使用
    makemigrations
    migrate
    命令

0
投票

正如错误所示,“venue_city”列中有一个空值。

在定义模型时,您似乎没有包含“null=True”。

因此它返回 null,因为您的“venue_city”变量中没有任何内容。考虑删除最后一个迁移文件并使用以下命令执行另一次迁移

python manage.py makemigrations
python manage.py migrate
命令。


0
投票

迁移已创建,但运行过程中失败。删除最后一次迁移。

如果您希望数据库中允许 null,请通过添加 null=True, default=None 来修复模型定义。

再次重新生成迁移(manage.py makemigrations场地)并迁移(manage.py migrate场地)。

© www.soinside.com 2019 - 2024. All rights reserved.