Supabase "duplicate key value violates unique constraint" (23505) in a Lovable or Bolt app: fixes (2026)
Prices and facts checked on each tool's official site. Plans change often, so confirm before you pay.
Your Lovable or Bolt app won't save, sign-up fails, or the browser console shows a 409 with this message from Supabase: duplicate key value violates unique constraint "profiles_pkey" and code 23505. It means the app tried to add a row that already exists. Here's how to find out why, and how to stop it for good.
What the error means
In the PostgreSQL error code list, 23505 is unique_violation. A column (or group of columns) with a unique constraint or primary key can't hold the same value twice, so Postgres refuses the insert. The message names the constraint, and the details say which value clashed, for example Key (id)=(…) already exists. Supabase's API returns this as HTTP 409 Conflict. The constraint name tells you where to look: one ending in _pkey is usually the table's primary key, others are usually unique columns like email or username.
Match the situation to the cause
| What you see | Most likely cause |
|---|---|
| Error only sometimes, after clicking Save or Submit | The button was clicked twice (or the form submitted twice), so the same row was inserted twice |
Error on sign-up, on the profiles table | A sign-up trigger already created the profile row, and the app inserts it again |
| Error when saving settings or a profile a second time | The code uses insert where it should use upsert with onConflict |
| Error after re-running seed or demo data | The seed script inserts rows that are already there |
…_pkey on new rows after you imported data | The id sequence is behind the highest id, so new rows reuse a taken id |
| Constraint name mentions email or username | Someone already uses that email or username |
1. Double clicks and double submits
If Save can be clicked twice before the first request finishes, the app sends two identical inserts. The second hits the unique constraint. Disable the button while saving (and show "Saving…"), and re-enable it when the request finishes or fails. If the row should only exist once per user, a unique constraint plus upsert makes a repeat click harmless.
2. Sign-up trigger plus an app insert
Supabase's docs recommend a database trigger that inserts a row into public.profiles every time a user signs up. If Lovable or Bolt also wrote code that inserts the same profile after sign-up, the second insert fails with profiles_pkey. Keep one of them: usually the trigger. Change the app's insert to an update or an upsert, and make the trigger safe to re-run by ending its insert with on conflict (id) do nothing. Supabase warns that a failing trigger can block sign-ups, so test sign-up after any change.
3. Use upsert with onConflict instead of insert
Supabase's .upsert() inserts the row if it doesn't exist and, if it does, takes an alternative action. onConflict takes the unique column(s) that decide what counts as a duplicate, for example .upsert({ user_id: user.id, theme: 'dark' }, { onConflict: 'user_id' }). By default the new values are merged into the existing row; set ignoreDuplicates: true to skip duplicates instead. The column in onConflict needs a unique constraint (or be the primary key). By default upsert doesn't return the row; chain .select() if the app needs it.
4. ON CONFLICT DO NOTHING in triggers and seed data
In SQL, Postgres's INSERT … ON CONFLICT DO NOTHING simply skips a row that would break a unique constraint instead of raising the error. For DO NOTHING the conflict column is optional; ON CONFLICT (col) DO UPDATE SET … updates the existing row and needs the conflict column. Add it to seed scripts you might run more than once and to sign-up triggers.
5. Reset an out-of-sync id sequence
Auto-numbered ids come from a sequence. If you imported a CSV or ran SQL that inserted rows with their own ids, the sequence doesn't move, so the next new row can get an id that's already taken and fail on _pkey. In the Supabase SQL editor, find the sequence with pg_get_serial_sequence (works for identity and serial columns) and move it past the highest id: select setval(pg_get_serial_sequence('public.items', 'id'), (select max(id) from public.items)); Replace items with your table. With the two-argument form of setval, the next new id is one higher than the value you set. Back up first, and only do this for number ids (uuid ids don't use a sequence).
6. Unique email or username: check first, show a friendly message
When the constraint is on email or username, the error is doing its job. Check whether the value is taken before saving (a quick select), and still handle a 23505 from the save, because two people can pick the same name at the same moment. Show "That username is taken" instead of a raw error.
What to paste into Lovable or Bolt
I get "duplicate key value violates unique constraint [name]" (code 23505) when [action]. Don't change any code yet. First tell me: which table and constraint it is, which code inserts into that table, whether a database trigger also inserts there (for example on sign-up), whether the save button can be clicked twice, and whether this should be an upsert with onConflict on the unique column. Then list the fixes in order. Don't drop the unique constraint, don't turn off Row Level Security, and don't use the service role key in browser code.
When it's time to get help
Often it's a double click or one insert that should be an upsert. Get a second pair of eyes when:
- New users can't sign up and you're losing them.
- The AI wants to delete the unique constraint to make the error go away.
- You imported data and aren't sure how to reset ids safely.
- You're about to launch and want the whole app checked.
| Option | Price | What you get |
|---|---|---|
| App Audit | $49 one-time | A plain-English written fix list for this error and anything else that breaks, within 3 business days |
| Payments Live | $199 one-time | Stripe payments wired into your app and tested with test cards before going live |
| Launch Pack | $499 one-time | Agreed bug fixes, Stripe payments, and your app published on your own domain with HTTPS |
We confirm the scope by email before any work starts, and you can ask questions first without paying. Never send anyone your password or your Supabase secret key.
Related guides
- Getting PGRST116 because of duplicate rows? See Cannot coerce the result to a single JSON object.
- Insert blocked by a policy instead? See the Supabase RLS error guide and Supabase's Oct 30 grants change.
- Not sure what your error means? Paste it into the free error decoder.
Sources
- PostgreSQL: error codes (23505 unique_violation)
- PostgreSQL source (exact message and "Key … already exists." detail)
- PostgREST: errors (23505 returned as HTTP 409)
- Supabase: upsert() (onConflict, ignoreDuplicates, chain .select())
- PostgreSQL: INSERT … ON CONFLICT (DO NOTHING, DO UPDATE, conflict target)
- PostgreSQL: sequence functions (setval)
- PostgreSQL: system information functions (pg_get_serial_sequence for identity and serial columns)
- Supabase: managing user data (profiles trigger on sign-up)
FAQ
What does error 23505 mean in Supabase?
Postgres error code 23505 is unique_violation: your app tried to save a row with a value that must be unique, like an id, email or username, and a row with that value already exists. The message reads duplicate key value violates unique constraint, followed by the constraint's name.
Should I use upsert instead of insert?
When the row may already exist and you want to update it, yes: call .upsert() with onConflict set to the unique column, for example user_id. By default upsert merges the new values into the existing row; with ignoreDuplicates: true it skips duplicates instead.
Why does the error mention _pkey?
The constraint ending in _pkey is usually the table's primary key. If you imported or seeded rows with their own ids, the id counter (sequence) can be behind the highest id in the table, so new rows reuse an id that's taken. Resetting the sequence with setval fixes it.
Why do new users get a duplicate key error on sign-up?
Often a database trigger already creates the user's profile row when they sign up, and then the app inserts the same row again. Remove the app's insert or switch it to upsert, and add ON CONFLICT DO NOTHING to the trigger's insert.
Some links are referral links; we may earn a commission at no extra cost to you. It never changes our recommendations.