relation "django_content_type" already exists django

mightycode Newton

oke, I have a django application. And I tried to update the models. So I did a

makemigrations and migrate. But that didn't worked. So I truncated the table django_migrations.

And I did a python manage.py makemigrations:

Migrations for 'zijnAdmin':
  zijnAdmin\migrations\0001_initial.py
    - Create model Category
    - Create model Animal

but when I try now: python manage.py migrate. I get this errors:

Running migrations:
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "C:\repos\DWL_backend\env\lib\site-packages\django\db\backends\utils.py", line 87, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "django_content_type" already exists

But I see that changes have been made in the tables. and the app works fine. But I still get this warning when I do a : python manage.py runserver 192.168.1.135:8000

You have 25 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): accounts, admin, auth, authtoken, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 24, 2023 - 15:49:58
Django version 4.2.3, using settings 'zijn.settings.local'
Starting development server at http://192.168.1.135:8000/
Quit the server with CTRL-BREAK.

Question: how to resolve this error?

Kiarash Gh
psycopg2.errors.DuplicateTable: relation "django_content_type" already exists

this error means that "contenttypes" app is trying to crate a table that already exists in data base.

before you do anything backup you database so you don't lose your data if anything goes wrong, then check the status of your migrations to see if there is any migrations that are not applied:

python manage.py showmigrations

make sure migrations are applied by running python manage.py migrate if you see any errors during the migration resolve them after running the migrate command check the status of migrations again using showmigrations command to make sure all migrations have been applied

if you have manually truncate the django-migrations table it must have led to some inconsistencies to resolve this issue you can try clearing the migrations for the "zijnAdmin" app and then create the initial migration again:

python manage.py migrate zijnAdmin zero
python manage.py makemigrations zijnAdmin 
python manage.py migrate

and since you truncate the django-migrations table it is possible that the django_content_type table was partially created to make sure that "contenttypes" app's migration runs fine you can try recreating the "django_content_type" table using this:

python manage.py migrate contenttypes --fake

the --fake flag tells the django to mark the migration as applied without actually running it

if these didn't work it's good idea to check the migration files, the state of your database and check any changes you made to the models

these are the things that came to my mind.

I hope this helps you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I resolve 'django_content_type already exists'?

How can I resolve 'django_content_type already exists'?

Django column "name" of relation "django_content_type" does not exist

Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist'

django.db.utils.ProgrammingError: relation "django_content_type" does not exist

Django project Migration from SQL lite to postgres - relation "django_content_type" does not exist

OperationalError: no such table: django_content_type and django_session

How can I get past this "'django_content_type' does not exist" error when deploying with Heroku?

Relation "table" already exists

django.db.utils.ProgrammingError: relation already exists on OenBSD vps

Django ProgrammingError: relation already exists after a migration created in the Django source code?

Django: relation "auth_user" already exists when executing manage.py test myApp

django.db.utils.ProgrammingError: column "image" of relation "choices_keyword" already exists

"django.db.utils.ProgrammingError: relation already exists" for unique together constraint

knex migrate throws error relation already exists

ERROR: constraint for relation already exists with hibernate 4.3.1

Liquibase - Error : relation "databasechangelog" already exists

South database error: relation already exists

Check if a OneToOne relation exists in Django

django.db.utils.ProgrammingError: relation "django_site_domain_v2339b81_uniq" already exists

Django South - table already exists

Django Check if Username already exists

Why am I getting relation already exists in this execute statement?

PostgreSQL Error: Relation already exists - FOREIGN KEY in CREATE TABLE

Vapor PostgreSQL Error: invalidSQL("ERROR: relation \"pages\" already exists\n")

Invalid operation: relation "fs_fsentry" already exists;

How to check if many-to-many relation already exists in create and update

Fix 'column already exists' Django Migration Error?

Django 1.8 migrations "Table already exists"