DBAtlas · Database operations atlas

23505: unique_violation

PostgreSQL

What does it mean?

The inserted or updated row violates a UNIQUE constraint or the PRIMARY KEY; the same value already exists in the table.

Why does it happen?

Re-inserting with the same unique key, entering duplicate data into a unique column, the sequence value falling behind because of manual inserts, or two concurrent requests inserting the same key.

How to fix it

The constraint name and key value in the error detail show which row conflicts. To update the existing row, use INSERT ... ON CONFLICT (my_column) DO UPDATE or DO NOTHING. To find duplicates: SELECT my_column, COUNT(*) FROM my_table GROUP BY my_column HAVING COUNT(*) > 1. If the sequence fell behind: SELECT setval('seq_name', (SELECT MAX(id) FROM my_table)).

Open in DBAtlas →

More from this engine