Django 1.8 migrate: django_content_type does not exist
Asked Answered
E

7

6

I just upgraded my django from 1.7.1 to 1.8.4. I tried to run python manage.py migrate but I got this error:

django.db.utils.ProgrammingError: relation "django_content_type" does not exist

I dropped my database, created a new one, and ran the command again. But I get the same error. Am I missing something? Do I need to do something for upgrading my django?

EDIT: I downgraded back to 1.7.1 and it works. Is there a way to fix it for 1.8.4?

Exurb answered 17/9, 2015 at 0:45 Comment(0)
E
1

Well, I found the issue. I have auditlog installed as one my apps. I removed it and migrate works fine.

Exurb answered 17/9, 2015 at 18:1 Comment(3)
Hey, how did you solve this problem in detail? I'm facing the exact same problem as u, but have no idea what the auditlog means here. Thank a lot!Andress
@JasonZhu auditlog is an external django app that was installed in my project. I cannot recall why it messed up the migration, but after I removed it from my settings, the migrations worked again. Have this in mind that my problem was solved because of a specific django app, so your project could be a whole different story.Exurb
OK! thanks for ur reply anyway, finally I found that my problem is related with postgres database privilege for user : )Andress
V
6

Delete all the migration folder from your app and delete the database then migrate your database......

if this does not work delete django_migration table from database and add the "name" column in django_content_type table ALTER TABLE django_content_type ADD COLUMN name character varying(50) NOT NULL DEFAULT 'anyName'; and then run $ python manage.py migrate --fake-initial

Varletry answered 17/9, 2015 at 5:30 Comment(0)
E
2

Here's what I found/did. I am using django 1.8.13 and python 2.7. The problem did not occur for Sqlite. It did occur for PostgreSQL.

I have an app the uses a GenericForeignKey (which relies on Contenttypes). I have another app that has a model that is linked to the first app via the GenericForeignKey. If I run makemigrations for both these apps, then migrate works.

Enshrine answered 6/5, 2016 at 13:50 Comment(0)
B
2

I had the same issue when trying to migrate our models from scratch (especially when testing builds). Upon further investigation, it turned out that we have custom codes related to ContentTypes model which is associated with the django_content_type table:

staff_content_types = ContentType.objects.filter(model__in=ACCOUNT_STAFF_APPS)

Then, in our succeeding codes that will access the staff_content_types variable, it will throw the django.db.utils.ProgrammingError: Table 'django_content_type' doesn't exist message. Apparently, it tries to access the django_content_type table which is yet to be built. And our code are based in the context that our data are already setup.

So, there are at least 2 workarounds possible. The idea is to run our custom ContentType-related logic only when our site has already data (after the migration phase):

  1. Catch the error which happens during migration and ignore it:

    from django.db.utils import ProgrammingError
    
    try:
        for content_type in staff_content_types:
            # Our custom codes here.
    except ProgrammingError:
        pass
    
  2. Proceed only if django_content_type table exists (which is the state after migration):

    from django.db import connection
    
    if 'django_content_type' in connection.introspection.table_names():
        for content_type in staff_content_types:
            # Our custom codes here.
    

Likewise, 3rd-party apps that utilize the Django ContentType objects/table might have similar issues. So, either you disable them or requests their authors to patch their contrib apps w/ the ideas suggested here.

Ber answered 1/8, 2017 at 11:35 Comment(1)
This is superb solution. Thank you!Fridlund
E
1

Well, I found the issue. I have auditlog installed as one my apps. I removed it and migrate works fine.

Exurb answered 17/9, 2015 at 18:1 Comment(3)
Hey, how did you solve this problem in detail? I'm facing the exact same problem as u, but have no idea what the auditlog means here. Thank a lot!Andress
@JasonZhu auditlog is an external django app that was installed in my project. I cannot recall why it messed up the migration, but after I removed it from my settings, the migrations worked again. Have this in mind that my problem was solved because of a specific django app, so your project could be a whole different story.Exurb
OK! thanks for ur reply anyway, finally I found that my problem is related with postgres database privilege for user : )Andress
H
0

i had drop the database and rebuild it, then in the PyCharm Terminal py manage.py makemigrations and py manage.py migrate fix this problem. I think the reason is the table django_content_type is the django's table, if it misssed can not migrate, so have to drop the database and rebuild.

Homeroom answered 12/5, 2021 at 10:8 Comment(1)
> drop the database :-DPhilina
M
0

Try to migrate contenttypes first, then the rest of the the models included with Django, then your own models:

./manage.py migrate contenttypes
./manage.py migrate
./manage.py makemigrations <YOUR APP>
./manage.py migrate --fake
Mice answered 13/9, 2021 at 5:27 Comment(0)
E
0

In my case the problem was that django_migrations table was in my database from previous testing django project. I entered mysql > droped the table in that database > then ran python manage.py migrate (where my project was) . migrations applied successfully. hoping it help someone

Edytheee answered 22/6 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.