How can I resolve 'django_content_type already exists'?

PythonDjango

Python Problem Overview


After upgrading to django 1.8 I'm recieving the error during migration:

ProgrammingError: relation "django_content_type" already exists

I'd be interested in the background behind this error, but more importantly, How can I resolve it?

Python Solutions


Solution 1 - Python

Initial migrations on a project can sometimes be troubleshot using --fake-initial

python manage.py migrate --fake-initial

It's new in 1.8. In 1.7, --fake-initial was an implicit default, but explicit in 1.8.

From the Docs:

> The --fake-initial option can be used to allow Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

Solution 2 - Python

I solved this issue on Django 2.2.7 or Django 3.0 hosted on Ubuntu 18.04 + Postgres 10.10 version.

> 1. Restore the database in Postgres database (used pgAdmin tool for this) > 2. (virtualenv)python manage.py loaddata dumpfile.json > 3. Dropping django_migrations table from database (used pgAdmin tool for this) > 4. (virtualenv)python manage.py makemigrations > 5. (virtualenv)python manage.py migrate --fake > 6. (virtualenv)python manage.py migrate > 7. (virtualenv)python manage.py collectstatic > 8. (virtualenv)python manage.py runserver 0.0.0.0:8000

Solution 3 - Python

I granted all privileges to the user on that specific database and it solved the issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan O'BoyleView Question on Stackoverflow
Solution 1 - PythonDan O'BoyleView Answer on Stackoverflow
Solution 2 - PythonSantanaView Answer on Stackoverflow
Solution 3 - PythonadriaanbdView Answer on Stackoverflow