Error: django.db.utils.ProgrammingError: (1146, "Table 'databasename.django_content_type' doesn't exist")

Hi! I’m building a website that uses Django as the backend and React.js as the frontend. I have an pre-existing database that I linked to my Django project. I then created the apps and generated the models for each app by using the inspectdb command. Finally I ran the makemigrations and migrate --fake commands and everything worked well. Recently I have decided to add user authentication to the website and I wanted to create the model for the app user_management where I would create the table for users. When I ran makemigrations, everything looked fine but when I ran the migrate command, I got the error in the title:
django.db.utils.ProgrammingError: (1146, “Table ‘databasename.django_content_type’ doesn’t exist”)

This is what my settings.py looks like:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    ...
]

From what I understand, the issue is because the table django_content_type was not created when I first ran the migrate command.

Is there a way to fix this?

Welcome @sofiateixeira22 !

When you have an app with a mix of tables that are managed and tables that aren’t, you don’t want to use --fake. Instead of using --fake, the more appropriate solution in this case is to use the managed = False in the Meta class of the models that you don’t want affected.

I think the easiest way forward at this point is to remove all rows from the migration table, add the managed = False to the unmanaged models, then rerun the migrations.

1 Like