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?

AliBZ
  • 4,039
  • 12
  • 45
  • 67

6 Answers6

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

Ashok Joshi
  • 428
  • 4
  • 13
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.

Chuck
  • 1,089
  • 1
  • 13
  • 18
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.

Ranel Padon
  • 525
  • 6
  • 13
1

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

AliBZ
  • 4,039
  • 12
  • 45
  • 67
  • 1
    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! – KAs Feb 01 '17 at 17:42
  • @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. – AliBZ Feb 02 '17 at 18:07
  • OK! thanks for ur reply anyway, finally I found that my problem is related with postgres database privilege for user : ) – KAs Feb 02 '17 at 23:33
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.

WoodPecker
  • 19
  • 4
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
Roger Dahl
  • 15,132
  • 8
  • 62
  • 82