Back to Blog

Fixing PostgreSQL “duplicate key value violates unique constraint” in Django

February 4, 2026
By Mahiuddin
2 min read
Backend Engineering
Django

The PostgreSQL “duplicate key value violates unique constraint” error in Django is frequently caused by a misaligned sequence, not bad data. Learn how to diagnose and fix it directly through Django.

duplicate key value violates unique constraint

One of the most confusing runtime errors you can encounter in a Django + PostgreSQL application is:

IntegrityError: duplicate key value violates unique constraint

At first glance, this error suggests that duplicate rows exist in the database. In many real-world cases, however, the data itself is perfectly fine. The real problem is often a PostgreSQL sequence that is out of sync.

Why This Happens

PostgreSQL uses sequences to generate auto-incrementing primary keys. Django relies on these sequences when inserting new records.

If records are:

  • Imported from backups
  • Inserted manually
  • Restored from dumps
  • Modified during migrations or testing

…the sequence can fall behind the maximum existing id in the table.

When Django attempts to insert a new row, PostgreSQL may reuse an already existing primary key, resulting in:

duplicate key value violates unique constraint "..._pkey"

Why sqlsequencereset Often Doesn’t Fix It

Django provides the command:

python manage.py sqlsequencereset my_app


However:

  • This command only prints SQL
  • It does not execute it
  • It often requires psql, which may not be available in containerized or CI environments

As a result, developers believe they’ve fixed the issue when nothing has actually changed.

Resetting the Sequence Without psql

A reliable solution is to reset the sequence directly through Django’s database connection. This approach works anywhere Django can connect to PostgreSQL and does not require external tools.

Example

psql.py
from django.db import connection
from my_app.models import ExampleModel


table = ExampleModel._meta.db_table
pk = ExampleModel._meta.pk.column


sql = f"""
SELECT setval(
pg_get_serial_sequence('{table}', '{pk}'),
COALESCE((SELECT MAX({pk}) FROM {table}), 1), true);
"""

with connection.cursor() as cursor:
cursor.execute(sql)

Run this code inside:

python manage.py shell

Why This Works

  • pg_get_serial_sequence automatically finds the correct sequence
  • MAX(id) ensures the sequence advances past existing rows
  • setval(..., true) marks the sequence as already used
  • No hardcoded table names
  • No dependency on psql

This is the same fix PostgreSQL expects, applied safely through Django.

When This Solution Applies

This fix works only when the failing constraint is the primary key, usually shown as:

... violates unique constraint "..._pkey"

If the constraint references:

  • A UUID field
  • A composite unique index
  • Any custom unique constraint

Then the issue is genuine duplicate data, not a sequence problem.


Last updated: February 4, 2026