Answers for "duplicate key value violates unique constraint "django_admin_log_pkey""

SQL
1

duplicate key value violates unique constraint "django_admin_log_pkey"

-- Enter the DB shell with `./manage.py dbshell`

-- Get latest ID
SELECT id FROM "django_admin_log" ORDER BY id DESC LIMIT 1;

-- Set the next value of the ID sequence to the result of the first query + 1
SELECT setval('django_admin_log_id_seq', LASTID + 1);

-- (replace `LASTID` with the result of the first query)
Posted by: Guest on December-28-2021
0

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()
Posted by: Guest on December-09-2020

Code answers related to "duplicate key value violates unique constraint "django_admin_log_pkey""

Code answers related to "SQL"

Browse Popular Code Answers by Language