django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"

This fix working for My postgres db

Open django shell

python manage.py shell

Run flowing python code to reset id

from django.db import connections

query = "SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations))"
cursor = connections['default'].cursor()
cursor.execute(query) 
row = cursor.fetchone()

After restarting the index, I indeed simply deleted/dropped the constraints and indices that were giving "already exists" errors and managed to migrate:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auditlog, auth, contenttypes, defender, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
  Applying lucy_web.0163_auto_20180627_1309... OK

Tags:

Python

Django