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?
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.
Comments