Skip to content
>_VibeUnstuck

violates foreign key constraint

Last verified:

The fix

insert or update on table violates foreign key constraint (23503). That id is missing from the parent. Use an existing id, or insert the parent first.

  1. 1

    Read the first words and the Key line

    insert or update on table means the new row names a parent that is not there. Copy the value in Key (column)=(value) and the parent table in is not present in table. update or delete on table, with is still referenced from table, means children still point at the row you tried to remove or change.

  2. 2

    For an insert, use a parent id that exists

    Look up the Key value in the parent table the detail names. A uuid pasted from a prompt will not be there. For a profile whose id references auth.users, use the signed-in user's id after sign-up has created that user. If you are creating both rows, insert the parent first, in the same transaction.

    -- The DETAIL named this parent. Use an id that is already there.
    select id
    from public.profiles
    where id = '<value from the Key line>';
  3. 3

    For a delete, decide what happens to the children

    Do not delete the parent to clear the error. Delete or reassign the child rows first, or add ON DELETE CASCADE only if losing those rows is what you want. Read the constraint name before you add CASCADE.

  4. 4

    If ALTER TABLE failed with this error

    Adding a foreign key checks the rows already in the table. Orphans fail the alter with the same 23503. Find them, fix or delete them, then add the constraint. Do not mark it NOT VALID and ship it. A null in the child column is allowed unless the column is also NOT NULL. That null error is a different page.

Why it happens

A foreign key says every non-null value in the child column has to match a row in the parent. Postgres checks that on insert and on update. The message is insert or update on table "orders" violates foreign key constraint "orders_user_id_fkey". The detail line is Key (user_id)=(…) is not present in table "profiles". The SQLSTATE is 23503. The Supabase client puts 23503 on error.code and that sentence on error.message.

The other direction is a delete or a parent-key change: update or delete on table "profiles" violates foreign key constraint, and the detail says the key is still referenced from table "orders". Generated apps hit the insert form with a sample uuid, or they insert an order before the profile row exists. A profile whose id references auth.users fails the same way when the id was never a real user. This is not Row Level Security. RLS says new row violates row-level security policy, and that shows up only after the parent id is valid. duplicate key value violates unique constraint is 23505.

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 23503. The full message is:
<paste insert or update on table ... violates foreign key constraint, or the update or delete form>

The DETAIL line is:
<paste Key (...)=(...) is not present in table, or is still referenced from table>

Do not disable the foreign key. Do not turn off RLS. Do not replace the id with a random uuid. Do not add ON DELETE CASCADE unless I say the child rows should disappear. Do not mark the constraint NOT VALID.

1. If the message starts with insert or update, name the parent table and tell me to insert that parent or use an id that already exists. Show a select for the Key value.
2. If it starts with update or delete, list the child rows that still reference it. Do not delete the parent as the fix.
3. If this happened while adding a foreign key, show the orphan query, then the alter table. Stop if the Key value is null. That is the not-null page.
4. Change only the insert, the delete, or that constraint.

Common questions

+The detail says is still referenced from table. Do I insert a parent row?

No. That form means you tried to delete or change a parent that child rows still use. Remove or reassign those child rows, or decide that ON DELETE CASCADE is actually what you want.

+Is violates foreign key constraint the same as duplicate key value violates unique constraint?

No. 23503 means the parent row is missing, or a parent still has children. 23505 means that exact key is already in this table. Read the constraint name. A primary key name is the duplicate. A name ending in _fkey is the foreign key.

+The app says new row violates row-level security policy. Is that this error?

No. RLS runs once the foreign key is happy. If you still see 23503, the id is not in the parent table, and a new policy will not create that parent row.

+Lovable inserted a uuid I never created. Why does Postgres reject it?

The foreign key checks that the value exists. A sample id from the prompt is not a row in profiles or in auth.users. Use the id sign-up returned, or insert the parent row first.

Related errors