Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
854 views
in Technique[技术] by (71.8m points)

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

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

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

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.

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

# -*- 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'),
        ),
    ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you added null=True after created migration file. Because venue_city is not a nullable field in your migration file

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...