App Finishers

Fix guide · updated 2026-11-16

Supabase "violates foreign key constraint" (23503)

{"code":"23503","details":"Key is not present in table \"posts\".","hint":null,"message":"insert or update on table \"comments\" violates foreign key constraint \"comments_post_id_fkey\""}

An insert or a delete from your app fails with code 23503 and HTTP 409. Either "insert or update on table "comments" violates foreign key constraint "comments_post_id_fkey"", or, when deleting, "update or delete on table "posts" violates foreign key constraint … on table "comments"". A foreign key ties a row to a row in another table, and the database refused to break that tie. We reproduced both with supabase-js 2.119, Supabase Auth and the Data API (PostgREST 13) on Postgres 16, November 2026.

What the error means

Look at how the message starts:

The constraint name usually tells you the column: comments_post_id_fkey is the post_id column of comments. With row level security (RLS) on, the details leave out the value. With the service role key, or on a table without RLS, they include it: "Key (post_id)=(999) is not present in table "open_posts"."

Saving: the row it points to isn't there

The id is wrong, the parent row was deleted, or it isn't created yet. Create the parent first and use the id it returns. In our test, a comment with post_id: 999 failed, and this worked:

const { data: post, error } = await supabase.from('posts').insert({ title }).select().single();
if (error) throw error;
await supabase.from('comments').insert({ post_id: post.id, body });

Saving: the user was deleted while still signed in

Deleting a user in the dashboard doesn't sign them out right away. The access token in their browser keeps working until it expires, an hour by default. In our test, the deleted user's next inserts failed on posts_user_id_fkey and profiles_id_fkey ("Key is not present in table "users""), and supabase.auth.getUser() answered 403 user_not_found, "User from sub claim in JWT does not exist".

This mostly happens while testing. Sign that browser out with supabase.auth.signOut() and sign up again. In the app, treat user_not_found like a signed-out user and send them to the login page.

Saving: a profile right after signUp()

With "Confirm email" on, signing up with an address that already has an account returns no error, no session, and a made-up user whose id isn't in auth.users. In our test, a profile insert with that data.user.id failed with 23503 using the service role key, and with "new row violates row-level security policy" from the browser, which has no session yet. A brand-new address was different: its user was in auth.users at once, before the email was confirmed, and the profile went in.

Create profiles with a database trigger on auth.users instead of from the app, and spot the made-up user with data.user.identities.length === 0: see Supabase "User already registered".

Deleting: children still point to the row

Decide what should happen to them, and tell the database:

To change an existing key, drop it and add it back with the rule:

alter table public.comments
  drop constraint comments_post_id_fkey,
  add constraint comments_post_id_fkey
    foreign key (post_id) references public.posts (id) on delete cascade;

You can also delete the children first, in your code. Check that RLS lets the user delete them: in our test, with no delete policy on comments, the delete returned no error and removed nothing, so the comment was still there to block the post.

The one-minute check

List every foreign key that points to a table, with its rule, in the SQL Editor:

select conrelid::regclass as child_table, conname, pg_get_constraintdef(oid)
from pg_constraint
where contype = 'f' and confrelid = 'public.posts'::regclass;

A key with no ON DELETE at the end blocks the delete. For a failed save, run a select with the id from your request on the table named in the details, and see whether the row is there.

If an AI tool built your app

Give your AI tool this prompt, with the message from your error:

My Supabase request fails with "violates foreign key constraint" (23503): [paste the message and details]. If it's an insert, make the code create the parent row first and use the id it returns, and create user profiles with a trigger on auth.users instead of right after signUp(). If it's a delete, tell me which tables point to this one and propose on delete cascade or on delete set null for each key, as a migration. Show me the SQL before running it.

Not quite your error?

Still stuck?

Run the failing insert or delete in the SQL Editor with the same values. The message there names the key and the value, and you can query both tables side by side until you see which row is missing or still linked.

Other Supabase problems? See Supabase fixes.