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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37241819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:16:38  来源:igfitidea点击:

django.db.utils.IntegrityError: column "venue_city" contains null values

sqldjangopostgresqldjango-modelsdjango-migrations

提问by Tony

I have read a lot of other posts here on stackoverflow and google but I could not find a solution.

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

It all started when I changed the model from a CharField to a ForeignKey. The error I recieve is:

当我将模型从 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

My model is as follows:

我的模型如下:

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

venue_country did not exist before so that migration happened successfully. But venue_city was a CharField.

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

I made some changes to my migration file so that it would execute the sql as follows:

我对迁移文件进行了一些更改,以便它按如下方式执行 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'''),

Thanks in advance!

提前致谢!

UPDATE: my new migration file:

更新:我的新迁移文件:

# -*- 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'),
        ),
    ]

回答by Anoop

Looks like you added null=Trueafter created migration file. Because venue_cityis not a nullable field in your migration file

看起来您是null=True在创建迁移文件后添加的。因为venue_city在您的迁移文件中不是可以为空的字段

Follow these steps.

跟着这些步骤。

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'

It should work

它应该工作

回答by 0n10n_

Had a similar problem i resolved it by removing the previous migration files.No technical explanation

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

回答by lapin

I solved it by just adding null = Trueto both the (automatically generated) migration file that was causing the issue and in the Model. Then migrateagain and your failed migration will now succeed. As you changed it also in your model, makemigrationwill detect no changes after that.

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

回答by DSAnup

I solved it by below:

我通过以下方式解决了它:

  1. First delete last migration that face problem
  2. Then add like venue_city = models.CharField(blank=True, null=True)
  3. Finally use makemigrationsand migratecommand
  1. 首先删除上次遇到问题的迁移
  2. 然后添加像 venue_city = models.CharField(blank=True, null=True)
  3. 最后使用makemigrationsmigrate命令