Supabase "duplicate key value violates unique constraint" (23505): how to fix it
Your Lovable, Bolt or Cursor app inserts a row and Supabase returns duplicate key value violates unique constraint with Postgres code 23505 (HTTP 409). Another row already holds that value in a column that allows each value once. The fix depends on intent: refuse it, replace it, or stop sending a duplicate in the first place.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Read which column collided
Postgres code 23505 (unique_violation) means another row already holds that value in a column that allows each value once. Supabase returns it as HTTP 409. The quoted constraint name points at the column: waitlist_email_key is table waitlist, column email; orders_pkey is the primary key.
const { error } = await supabase
.from('profiles')
.insert({ id: user.id, username })
// error.code === '23505'
// error.message: duplicate key value violates unique constraint "profiles_username_key"
To see exactly what a constraint covers:
select tablename, indexdef
from pg_indexes
where indexname = 'profiles_username_key';
2. Decide: refuse, replace, or stop sending it
Refuse it when a second row would be a mistake (username taken, email already registered). Keep the constraint and show your own sentence — the raw message can echo the value back to the visitor:
if (error?.code === '23505') {
setMessage('That username is taken. Try another one.')
} else if (error) {
setMessage('Something went wrong. Please try again.')
}
Replace it when the newest version should win (one settings row per user). That is an upsert, and onConflict must name the unique column from the error:
const { error } = await supabase
.from('profiles')
.upsert(
{ id: user.id, username, updated_at: new Date().toISOString() },
{ onConflict: 'id' }
)
From the browser, upsert also needs a policy that lets the user update their own row. An insert-only policy is why upsert still returns 23505 or a silent no-op.
3. Double-taps and race conditions
Two clicks on "Save" often send two inserts before either finishes. Disable the submit button while the request runs. For "get or create" flows, prefer .maybeSingle() plus a unique constraint, and treat 23505 as "someone else just created it" — see PGRST116 / JSON object requested.
const { error } = await supabase.from('bookings').insert({ /* … */ })
if (error?.code === '23505') {
// fall through to update the existing row
}
4. "Key (id)=(1) already exists" when you sent no id
After an import or a manual insert that set id by hand, the serial/identity counter can sit behind the highest row. The next insert with no id reuses a taken number. Reset it once:
select setval(
pg_get_serial_sequence('public.todos', 'id'),
(select coalesce(max(id), 1) from public.todos)
);
Do not drop the primary key to "fix" it.
5. Triggers that already created the row
If a handle_new_user trigger inserts into public.profiles on signup, your app's own profile insert will hit 23505 on id. Update the row the trigger made, or upsert on id. See Database error saving new user if signup itself fails.
Quick checklist
- Read the constraint name → which column
- Unique-by-design values: catch
23505and show a friendly message - One-row-per-user: upsert with the right
onConflict+ UPDATE policy - Disable the button while saving
- Serial behind after import:
setval, don't drop the PK
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.