Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 migrate: django_content_type does not exist

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?

like image 377
AliBZ Avatar asked Sep 17 '15 00:09

AliBZ


3 Answers

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

like image 90
Ashok Joshi Avatar answered Sep 21 '22 01:09

Ashok Joshi


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.

like image 23
Chuck Avatar answered Sep 20 '22 01:09

Chuck


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.

like image 44
Ranel Padon Avatar answered Sep 19 '22 01:09

Ranel Padon