Skip to content
>_VibeUnstuck

duplicate key value violates unique constraint

Last verified:

The fix

Postgres already has a row with this primary key or unique value. The SQLSTATE is 23505. PostgREST returns it as HTTP 409. Inserting the same profile or order again will keep failing.

  1. 1

    Read the constraint name and the Key line

    The constraint name is in the message. The detail names the column and the value that already exists. In the Supabase client, error.code is "23505" and error.message is the Postgres sentence. Do not retry the same insert in a loop. Do not drop the constraint from the Table Editor.

  2. 2

    Decide who is allowed to insert that row

    If a trigger on auth.users already creates the profile, delete the client insert and select the row. If the client is the only writer, insert once, when the user is created, not on every render of the dashboard. Two writers need one statement that can run twice. upsert with onConflict set to the constrained column updates the existing row or leaves it, depending on the option. The conflict target has to be a unique index or a primary key. id usually is.

    const { error } = await supabase.from("profiles").upsert(
      { id: user.id, email: user.email },
      { onConflict: "id" },
    );
    if (error) throw error;
  3. 3

    Treat a retried webhook as already stored

    When the unique column is the Stripe event id or the Checkout session id, catch only 23505 and return success. The row from the first delivery is the one you keep. Any other code, including 42501 and 23502, is still a failure and should be a non-200 so you can see it. Do not ignore every error from the insert.

  4. 4

    Confirm with the value from the detail line

    Select the existing row by the column named in Key (...). You should see one row. Submit the form again. A second submit should not add a row and should not toast the raw constraint name. If you needed a second row, the column in the constraint is the wrong unique key, and the fix is a different column in a new migration, not deleting profiles_pkey.

Why it happens

Postgres rejects the insert with duplicate key value violates unique constraint "profiles_pkey". The detail line is Key (id)=(…) already exists. The SQLSTATE is 23505. PostgREST's error table maps 23505 to HTTP 409 and forwards that message. The constraint name tells you which rule fired: a primary key (profiles_pkey), a unique column (users_email_key), or a unique pair you added yourself.

Generated sign-up does it with two writers. A trigger on auth.users inserts the profile, and the page also inserts a profile with the same id after signUp. The second insert hits profiles_pkey. The same page on a later visit inserts again instead of reading. Stripe webhook handlers that insert an order on every delivery hit a unique event_id or session_id when Stripe retries. That collision means the first delivery was stored. It is not a failed payment.

This is not Row Level Security. RLS says new row violates row-level security policy. A missing column is PGRST204 or 42703. A missing parent row is foreign key 23503. A null in a NOT NULL column is 23502. Dropping the unique constraint makes the 409 stop and lets the table store two owners for one user.

The next error you'll probably hit

Once this is fixed, the usual follow-up is new row violates row-level security policy. new row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.

Prompt to paste back into your AI tool

Paste this above the error.

Postgres returns 23505: duplicate key value violates unique constraint "CONSTRAINT". Detail: Key (COLUMN)=(value) already exists. PostgREST status is 409 if it came through the Data API. The insert is:
<paste the insert or upsert>

Do not drop the primary key or the unique constraint. Do not disable RLS. Do not catch every error and continue. Do not insert the same profile from a trigger and from the client.

1. Name the constraint and the column in the Key line, and say whether a trigger also writes this table.
2. If the row is supposed to exist already, stop inserting it on each page load. Select it, or upsert on that conflict target.
3. If this is a webhook retry on a unique event id, handle only code 23505 as already stored and return success. Leave other errors failing.
4. Change only that write.

Common questions

+Sign-up creates the user and then fails on the profile insert. Why?

The auth user was created, and something already inserted a profile with that id. A trigger on auth.users is the usual first writer. The page's insert is the second. Keep one writer, or upsert on id. The login still exists. Only the extra insert failed.

+The code is 23505 and the message says new row violates row-level security. Can both be true?

Those are different responses. 23505 is the unique-constraint sentence. The RLS sentence is new row violates row-level security policy, and it does not use 23505. Handle the code you actually got. A table can hit RLS on one request and a duplicate key on the next, after the policy starts allowing the insert.

+Will onConflict: 'id' work if id is not unique?

No. Upsert needs a unique index or primary key on the conflict target. profiles.id is unique when it is the primary key. A plain column with duplicate values cannot be the conflict target until a unique index exists, and adding that index fails if duplicates are already stored.

Related errors