App Finishers

Fix guide · updated 2026-11-16

Supabase: column reference "id" is ambiguous (42702)

{"code":"42702","details":"It could refer to either a PL/pgSQL variable or a table column.","hint":null,"message":"column reference \"id\" is ambiguous"}

Calling a database function with supabase.rpc() fails with column reference "id" is ambiguous, code 42702, HTTP 400, often with "It could refer to either a PL/pgSQL variable or a table column." Inside the function, one name means two things at once, and Postgres refuses to guess. The fix is a small change to the function. We tested each case and each fix on Postgres 16 behind Supabase's Data API (PostgREST 13, November 2026).

What the error means

In a PL/pgSQL function (language plpgsql), the parameters and the columns listed in returns table (...) are variables. When a query in the function uses a table column with the same name, Postgres can't tell which one you mean. In our test:

In a PL/pgSQL function, the error shows up when the function runs, not when you create it: the migration passes, and the app fails later.

The one-minute check

The message names the column. Look at the function's code in the SQL Editor:

select pg_get_functiondef('public.my_profile'::regproc);

Find that name in three places: the parameters, the returns table (...) list, and the queries inside. Two of them sharing it is your clash.

Fix 1: put the table in front of every column

Give the table a short alias and use it on every column:

create or replace function public.my_profile(p_user uuid)
returns table (id uuid, name text)
language plpgsql stable as $$
begin
  return query select p.id, p.name from public.profiles p where p.user_id = p_user;
end $$;

In our test this version returned the profile. It's the safest fix: every name says where it comes from.

Fix 2: one line that says "columns win"

Add #variable_conflict use_column as the first line of the body, before begin. Clashing names then mean the column:

create or replace function public.my_profile(p_user uuid)
returns table (id uuid, name text)
language plpgsql stable as $$
#variable_conflict use_column
begin
  return query select id, name from public.profiles where user_id = p_user;
end $$;

In our test the original query worked with just this line added.

Fix 3: a plain SQL function

If the body is a single query, write it in language sql, which has no such variables. In our test the same query as a SQL function worked:

create or replace function public.my_profile(p_user uuid)
returns table (id uuid, name text)
language sql stable as $$
  select id, name from public.profiles where user_id = p_user;
$$;

Parameters named like columns

Give parameters a prefix, like p_user_id, so they can never clash. create or replace can't rename a parameter: in our test it answered "cannot change name of input parameter", so run drop function public.get_profile(uuid); first. The new name is also what your app must pass: supabase.rpc('get_profile', { p_user_id: user.id }).

Watch out for the SQL version of this mistake: it doesn't fail. In a SQL function, the column wins without any error. In our test, a function with a parameter user_id and where user_id = user_id returned every row in the table, not just that user's. Rename the parameter there too.

Two tables with the same column

When a query joins two tables that both have id (or name, created_at), write which one you mean: profiles.id or teams.id. In a SQL function, this clash shows up at creation: in our test, create function itself was refused with the same message.

If an AI tool built your app

Give your AI tool this prompt, with your function's name:

The Supabase function public.my_profile fails with column reference "id" is ambiguous (42702). Rewrite it so every column in its queries is qualified with a table alias (p.id, p.name), and give parameters that share a column's name a p_ prefix (drop the function first: create or replace can't rename parameters), updating the supabase.rpc() calls in the app to the new names. Keep the same return columns. Show me the SQL before running it. Don't change anything else.

Not quite your error?

Still stuck?

Run the function in the SQL Editor with real values, like select * from public.my_profile('<a user id>');. Postgres then shows the query and the line where the name clashes, for example CONTEXT: PL/pgSQL function my_profile(uuid) line 3 at RETURN QUERY.

Other Supabase problems? See Supabase fixes.