Supabase “duplicate key value violates unique constraint” on upsert – the five causes

By Abheesht Tripathi · Published

duplicate key value violates unique constraint "profiles_email_key"  (code 23505, HTTP 409)
DETAIL: Key (email)=(sam@example.com) already exists.

What it means

A unique constraint or unique index (named in the message) already has a row with that value, and your statement tried to create a second one. With upsert this feels wrong, because upsert is supposed to update the existing row. It only does that when the conflict happens on the column(s) the upsert is told to watch.

1. The conflict is on a column upsert isn't watching

supabase-js upsert() resolves conflicts on the primary key by default. If you upsert { email, name } without an id, Postgres tries an insert, the unique email collides, and you get 23505. Name the conflict column:

const { data, error } = await supabase
  .from('profiles')
  .upsert(
    { email: 'sam@example.com', display_name: 'Sam' },
    { onConflict: 'email' },          // a column (or 'col_a,col_b') with a unique constraint
  )
  .select();

To skip existing rows instead of updating them, add ignoreDuplicates: true (Postgres on conflict do nothing).

2. onConflict names a column without a unique constraint

Then the error changes to there is no unique or exclusion constraint matching the ON CONFLICT specification (42P10). Add the constraint. A partial unique index (... where deleted_at is null) won't match either, because the upsert can't send the index's where clause; use a full unique constraint for upsert targets.

alter table public.profiles add constraint profiles_email_key unique (email);

3. The id sequence is behind the data

If the failing constraint is the primary key (items_pkey, Key (id)=(5) already exists) on a plain insert, rows were imported with explicit ids and the sequence still hands out old numbers. Move it past the highest id:

select setval(
  pg_get_serial_sequence('public.items', 'id'),
  coalesce((select max(id) from public.items), 0) + 1,
  false
);

This works for serial and identity columns.

4. The same key twice in one batch

Upserting an array where two objects share the conflict key fails with a different message: ON CONFLICT DO UPDATE command cannot affect row a second time (21000). Deduplicate before sending:

const unique = [...new Map(rows.map((r) => [r.email, r])).values()];
await supabase.from('profiles').upsert(unique, { onConflict: 'email' });

5. Two requests at the same moment

A double-clicked button or two tabs can both check “does it exist?”, see no, and both insert. Don't check-then-insert; let the database decide with an upsert on the unique column, or treat error.code === '23505' as “already done”.

Upsert also needs the right policies

With RLS on, an upsert needs INSERT, UPDATE and SELECT policies, even with ignoreDuplicates and whether or not you call .select(). A missing UPDATE policy shows up as “new row violates row-level security policy”, not 23505.

How to verify

List the unique constraints and indexes, and make sure your onConflict matches one exactly:

select conname, pg_get_constraintdef(oid)
from pg_constraint
where conrelid = 'public.profiles'::regclass and contype in ('p', 'u');

select indexname, indexdef
from pg_indexes
where schemaname = 'public' and tablename = 'profiles';

Run the upsert twice with the same data: the first call inserts, the second updates, and neither returns 23505.

Related guides

More in Supabase RLS and Storage · all guides