22

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'),
        ),
    ]
Tony
  • 2,382
  • 7
  • 32
  • 51
  • 2
    I think, you added `null=True` after created migration file. Because `venue_city ` is not a nullable field in your migration file – Anoop May 15 '16 at 19:23
  • 1
    @Anoop how can i fix this? and yes I did. – Tony May 15 '16 at 19:25
  • which django version you are using? – Anoop May 15 '16 at 19:25
  • If it is latest version then, execute again `python manage.py makemigration – Anoop May 15 '16 at 19:30
  • 1.8.7, I tried doing the makemigration and migration again but still the same – Tony May 15 '16 at 19:37
  • could you please share your migration file `alter query`? – Anoop May 15 '16 at 19:40
  • I updated my question with the new migration file – Tony May 15 '16 at 19:48
  • Looks like you created another migration file for remove `venue_city`. If this is new table in prod then delete tall these migration files and generate new one – Anoop May 15 '16 at 20:00
  • If this is an existing table in prod then, remove all the migration files from your local since you started this `CharField to a ForeignKey` changes and create new migration file. – Anoop May 15 '16 at 20:03
  • I get django.db.migrations.graph.NodeNotFoundError: Migration venues.0018_auto_20160514_2146 dependencies reference nonexistent parent node ('venues', '0017_remove_venue_venue_city') - when i do makemigration – Tony May 15 '16 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111999/discussion-between-tony-kyriakidis-and-anoop). – Tony May 15 '16 at 20:04

6 Answers6

30

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

Anoop
  • 1,415
  • 11
  • 20
7

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

0n10n_
  • 382
  • 3
  • 10
  • just leave the first one[0001].just save the in another folder incase it dosent work. – 0n10n_ May 15 '16 at 19:49
  • did that, It requests dependencies from other nodes when i run makemigrations – Tony May 15 '16 at 20:02
  • :-( my help ends there.But because migration files are Dependant with previous files.Try inspecting them because the error is a result of conflicting commits to the database – 0n10n_ May 15 '16 at 20:10
  • 3
    Definitely don't do this blindly. Could result in some crazy prod db issues. – dster77 Sep 25 '18 at 23:29
6

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

lapin
  • 2,098
  • 2
  • 21
  • 30
2

follow the below steps:-

  • add null=True, blank=True in Venue model class eg: venue_city = models.ForeignKey(City, null=True, blank=True)
  • delete venues.0016_auto_20160514_2141 migration file from migrations folder in your app
  • then run python manage.py makemigrations
  • then run python manage.py migrate

no need to drop table or remove migrations from migration tables

0

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 makemigrations and migrate command
B. Cratty
  • 1,725
  • 1
  • 17
  • 32
DSAnup
  • 23
  • 7
0

As the error says, you have a null value in the "venue_city" column.

It seems at the time of defining your model you had not included "null=True".

So it returns null because there is nothing in your 'venue_city" variable. Consider deleting the last migration file and perform another migration using python manage.py makemigrations and python manage.py migrate commands.

Kibiku Brian
  • 221
  • 2
  • 11