duplicate key value violates unique constraint
- Postgres
- Supabase
What it means
The database refused to save a second row with a value that must be unique. Often the same action ran twice: a double click, a retry, or a webhook delivered again.
Why it happens
- A button that can be pressed twice before the first save finishes.
- A webhook or job that runs again after a timeout.
- Code that checks 'does it exist?' and then inserts, racing another request.
How to fix it
- Make saves idempotent: use upsert, or insert with 'on conflict do nothing' where a repeat is harmless.
- Record processed event ids so webhook retries are skipped.
- Disable the button while it is saving.
Let your AI find the cause
Paste this into Claude Code, Cursor, Lovable or whichever AI built your app. It looks first and changes nothing until you have read the answer.
My app sometimes fails with 'duplicate key value violates unique constraint'. Without changing anything yet, find the insert behind it, tell me which actions can trigger it twice (double clicks, retries, webhooks), and whether a repeat should create a second row at all.