Category: Supabase and databases

PostgreSQL 23505: Duplicate Key Violates Unique Constraint

Read this guide in Polish

Data insertion or synchronization stops at SQLSTATE 23505. The UNIQUE or PRIMARY KEY index detected a repeated value which the data model considers to be unique.

How to determine the source of the error

Read the name of the breached contraint and columns specified in the error details.

See if it's a real duplicate, a webhook rerun, or a conflict of ID numbering.

What to do step by step

1. For idmpotent imports, use a public ON CONFLICT with a specific business key and proper update semantics.

2 . If the data should not be unique in this respect, redesign the index after checking the requirements instead of shutting down all restrictions.

How to check the score

Reprocessing the same message does not duplicate and does not destroy previous data.

What to do

Do not use ON CONFLICT TO NOTTING for all errors without recording them.