Django 1.8 migrate - relation "django_content_type" already exists
Asked Answered
E

4

6

Any idea why I would be getting this error when I try to migrate?

django.db.utils.ProgrammingError: relation "django_content_type"

I am using using Django 1.8 & PostgreSql

Employ answered 16/6, 2015 at 18:58 Comment(2)
Removed Thanks and Sig. Added appropriate tags.Indelicacy
Possible duplicate of How can I resolve 'django_content_type already exists'?Conglomeration
D
8

You're going to need to use the --fake-initial option when you migrate; it used to be implicit, but has now been made explicit:

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

To quote:

This option is intended for use when first running migrations against a database that preexisted the use of migrations.

Good luck!

Dickens answered 16/6, 2015 at 19:35 Comment(4)
Thanks for your help @Dickens but it the migration still doesn't happen. Now I see: django.db.utils.ProgrammingError: column "name" of relation "django_content_type" does not exist . Any ideas? Is it simply a matter of manually creating it?Employ
I'm not sure exactly what the problem was, but in Django 1.8, the "name" field on on ContentType has become a property. This caused us issues when migrating to 1.8 in development while some people were still using 1.7. docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/… Hopefully, that might help point you in the right direction?Dickens
Thanks again mate, i think my migrations are hosed and i have a bit of manual work to do on the db. Will check out the link. Cheers!Employ
Or just drop your entire database tables and start over.Conglomeration
A
7

This worked for me:

  1. ./manage.py migrate auth --fake-initial (this throws an error but ignore it)

  2. ./manage.py migrate --fake-initial

Abutter answered 24/8, 2015 at 10:31 Comment(2)
This worked for me. But why? Can you please explain me??Carisacarissa
@Carisacarissa sadly no. i've not dug deep enough to understand what the problem is. it only happens when i'm migrating crusty old pre-1.6 code so I just moved on to bigger things once it worked.Abutter
N
6

I, like many, don't really understand the problem. I was able to devise a solution that worked for me.

  1. Comment out all of your custom installed apps. So, you have just the Django stuff.
  2. Migrate only auth: ./manage.py migrate auth
  3. Migrate everything else: ./manage.py migrate
  4. Undo step 1.
  5. Migrate all your apps: ./manage.py migrate.

Good luck!

Nones answered 2/8, 2015 at 20:22 Comment(0)
P
4

@Josh's solution worked for me with the following changes. Prior to step 1, I re-added the missing column:

ALTER TABLE django_content_type ADD COLUMN name character varying(50) NOT NULL DEFAULT 'run migrate.py';

Running python manage.py migrate auth removes this column, presumabley making one or more other changes that failed on some earlier run of migrate.

When running migrate in step 3 I included the --fake-initial flag:

python manage.py migrate --fake-initial

Everything seems to be set right again.

Paver answered 1/5, 2016 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.