App Finishers

Fix guide · updated 2026-11-16

Supabase "null value in column … violates not-null constraint" (23502)

{"code":"23502","details":null,"hint":null,"message":"null value in column \"user_id\" of relation \"notes\" violates not-null constraint"}

An insert or update from your app fails with "null value in column "user_id" of relation "notes" violates not-null constraint", code 23502, HTTP 400. The column is marked not null, and the row reached it empty. The message names the column and the table. We reproduced the usual ways it happens with the request code of supabase-js (postgrest-js 2.119) against Postgres 16 and Supabase's Data API (PostgREST 13), November 2026.

What the error means

A not null column refuses an empty value. The row got one because your code left the column out and it has no default, because your code sent null, because the default itself gave null, or because supabase-js filled in null for you. The causes below cover each case.

On most Supabase tables, row level security (RLS) is on, and then the error's details are empty. With the service role key, or on a table without RLS, they show the row: "Failing row contains (3, null, hi)."

One exception: if the empty column is the one your RLS policy checks, such as user_id = auth.uid(), you get "new row violates row-level security policy" instead. In our test, the policy check came first. See Supabase RLS: "new row violates row-level security policy".

Cause 1: the column isn't sent and has no default

The code inserts { body } into a table whose user_id is not null with no default. In our test, with the service role key that's 23502 on user_id. Watch out for user_id: user?.id while the user hasn't loaded yet: undefined values are dropped from the request, so supabase-js sent exactly {"body":"hi"}, the same as leaving the column out.

Cause 2: your code sends null

A default only fills a column that the insert leaves out. It never replaces a null you send. In our test, done had the default false, and insert({ title, done: null }) still failed on done. So did update({ done: null }).

The same goes for id: null. On an id column created as generated by default as identity, it failed with 23502 on "id". On one created as generated always, it failed with "cannot insert a non-DEFAULT value into column "id"" (code 428C9). Leave id out of inserts and the database numbers the row.

Cause 3: several rows with different keys

When you insert an array, supabase-js sends one list of columns for the whole batch, every key that appears in any row. A row that doesn't have one of those keys gets null for it, not the default. In our test, insert([{ title: 'd' }, { title: 'e', done: true }]) failed on done for the first row, even though done has a default.

Cause 4: an upsert to change one field

upsert() is an insert that turns into an update when the row exists, and the row to insert has to be complete. In our test, upsert({ id: 1, done: true }) to tick an existing todo failed with 23502 on title, although the row with id 1 was there.

Cause 5: auth.uid() as the default, and nobody signed in

default auth.uid() fills the column with the signed-in user's id. With no session, auth.uid() is null. In our test, a guestbook table whose insert policy lets everyone write failed with 23502 on user_id for a visitor who wasn't signed in. With a policy for signed-in users only, the same request got the RLS error instead.

The one-minute check

Note the column and the table in the message. Then, in the SQL Editor, see which columns are required and which have a default:

select column_name, is_nullable, column_default
from information_schema.columns
where table_schema = 'public' and table_name = 'todos'
order by ordinal_position;

Then open the failing request in your browser's Network tab and look at what was sent. The column is missing (cause 1 or 5), it's null (cause 2), the body is an array (cause 3), or the request has Prefer: resolution=merge-duplicates, which is what upsert() sends (cause 4).

Fix 1: give owner columns a default

For a column that holds the owner, let the database fill it:

alter table public.notes alter column user_id set default auth.uid();

In our test, insert({ title }) by a signed-in user then got the user's id in user_id, and the RLS policy still checked it. Leave the column out of the insert: sending user_id: null still fails.

Fix 2: wait for the user, then send the real value

Don't insert before the session has loaded. Read the user first, and stop if there isn't one:

const { data: { user } } = await supabase.auth.getUser();
if (!user) return; // show the login screen instead
const { error } = await supabase.from('notes').insert({ body, user_id: user.id });

Fix 3: batches with defaults

Pass defaultToNull: false, or give every row the same keys. In our test, the batch that failed went in with the option: supabase-js asked for the defaults (Prefer: missing=default) and the first row got done = false.

await supabase.from('todos').insert(rows, { defaultToNull: false });

Fix 4: change one field with update()

To edit a row that exists, use update() with a filter. In our test, it changed the one field and nothing else. An upsert() worked too once it carried every required column.

await supabase.from('todos').update({ done: true }).eq('id', id);

Fix 5: allow empty, if empty is fine

If the column can really be empty, such as an optional note, drop the rule instead:

alter table public.todos alter column note drop not null;

Don't do this for owner columns like user_id: a row without an owner is a row nobody's policies can reach.

If an AI tool built your app

Give your AI tool this prompt, with the column and table from your error:

My Supabase insert fails with "null value in column "user_id" of relation "notes" violates not-null constraint" (23502). Find every insert, upsert and update on that table. Don't send null or undefined for that column: give owner columns a default of auth.uid() in a migration and leave them out of the insert, wait for the signed-in user before inserting, leave id out of inserts, use update().eq('id', id) to change one field instead of upsert(), and pass { defaultToNull: false } when inserting an array of rows with different keys. Show me each change before making it.

Not quite your error?

Still stuck?

Copy the request body from the Network tab and try the same insert in the SQL Editor with those values. Change one value at a time until the row goes in: the last one you changed is the empty one.

Other Supabase problems? See Supabase fixes.