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?
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
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.
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):
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With