django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey" DETAIL: Key (id)=(291) already exists code example
Example: duplicate key value violates unique constraint "django_migrations_pkey"
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()