VibeCPR

← All fixes

Supabase · RLS · supabase-js

Fix Supabase .single() "JSON object requested, multiple (or no) rows returned" (PGRST116)

Updated · Applies to: Supabase, Lovable, Bolt, v0, Cursor, Replit, Claude Code

TL;DR fix: .single() asks the API for exactly one row. Any other count returns HTTP 406 with code PGRST116 ("JSON object requested, multiple (or no) rows returned", or "Cannot coerce the result to a single JSON object" on newer PostgREST). Read error.details: 0 rows means the row is missing or RLS is hiding it from this user (very common: the SQL editor shows it, the app does not); 2+ rows means your filter is not unique. Fix the policy, the session or the filter. Use .maybeSingle() only when "no row" is a legitimate answer.

Steps

  1. Read error.details, not just the message. Log error.code, error.message and error.details. The details say how many rows came back, for example 'The result contains 0 rows' or 'Results contain 2 rows'. Zero and two-plus are different bugs.
  2. Zero rows: check whether the row exists at all. Run the existence query below in the SQL editor. The SQL editor runs as postgres, which bypasses RLS, so if the row shows up there but not in the app, RLS or a missing session is hiding it.
  3. Zero rows but the row exists: test as the app's role. Use the impersonation block below to run the same query as authenticated with the user's id in request.jwt.claims. If it returns nothing, the table has no SELECT policy that matches this user. Also check the request actually carries the user's session; without it the request runs as anon and auth.uid() is null.
  4. Fix the SELECT policy (scoped, never USING (true)). Add or correct a SELECT policy such as using ((select auth.uid()) = user_id) for authenticated. Do not 'fix' PGRST116 by making the table readable to everyone.
  5. Two or more rows: make the filter unique. Filter on the primary key or a column with a unique constraint. If duplicates are a data bug (two profile rows for one user), clean them up and add a unique constraint so it can't happen again.
  6. Use maybeSingle() where no row is normal. For optional rows (settings not created yet, first login), .maybeSingle() returns data null with no error for 0 rows. It still errors with PGRST116 for 2+ rows.

What the error actually says

{
  "code": "PGRST116",
  "message": "JSON object requested, multiple (or no) rows returned",
  "details": "The result contains 0 rows",
  "hint": null
}

Newer PostgREST releases changed the message to Cannot coerce the result to a single JSON object (PR #3795); the code PGRST116 and HTTP status 406 are the same. Match on error.code, then read details for the count.

.single() is not a filter. It tells the API “send me exactly one row as an object”. Supabase’s reference says the result must be one row, “otherwise this returns an error”. Remove .single() and the same query returns [] or a list with no error, which is a fast way to see what’s really coming back.

0 rows: missing, or hidden by RLS?

Run this in the SQL editor (replace the table and id). The SQL editor bypasses RLS, so this tells you whether the row exists at all:

-- 1. Does the row exist? (runs as a privileged role, ignores RLS)
select * from public.profiles where id = 'USER-UUID-HERE';
  • No row: the data is genuinely missing. Typical cause: the profile row was never created at signup. Create it (for example with a trigger on auth.users, as Supabase’s docs show) or use .maybeSingle() and handle null.
  • Row exists: RLS or the session is hiding it. Continue.
-- 2. Is RLS on, and which SELECT policies exist?
select c.relname as table, c.relrowsecurity as rls_on
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relname = 'profiles';

select policyname, cmd, roles, qual
from pg_policies
where schemaname = 'public' and tablename = 'profiles'
  and cmd in ('SELECT', 'ALL');

RLS on with no SELECT (or ALL) policy means nobody using the API can read any row. That’s the most common cause.

-- 3. Run the query as the app's signed-in user would.
begin;
set local role authenticated;
select set_config('request.jwt.claims',
  '{"sub":"USER-UUID-HERE","role":"authenticated"}', true);
select * from public.profiles where id = 'USER-UUID-HERE';  -- 0 rows = RLS hides it
rollback;

If step 3 returns nothing, the policy doesn’t match this user. If it returns the row, the policy is fine and the app request isn’t carrying the user’s session: it ran as anon, where auth.uid() is null. That happens when you query before the session is restored on page load, or use a client created without the user’s session.

Fix: a scoped SELECT policy

alter table public.profiles enable row level security;

create policy "Users can read their own profile"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = id );   -- or = user_id, whatever your ownership column is

Don’t fix PGRST116 with using (true). That makes every row readable to everyone the policy names. See why USING (true) makes a table public.

2+ rows: the filter isn’t unique

-- Find duplicates for the column you filter on
select user_id, count(*)
from public.profiles
group by user_id
having count(*) > 1;

Filter on the primary key, or clean up the duplicates and stop new ones:

alter table public.profiles add constraint profiles_user_id_key unique (user_id);

Use .limit(1) only if any one of several matches is genuinely fine (the Supabase reference’s own .single() example uses .limit(1)).

single() vs maybeSingle()

// Row must exist (a profile every user has): keep .single(), surface the error
const { data, error } = await supabase
  .from('profiles').select('*').eq('id', user.id).single()

// Row may not exist yet (optional settings): .maybeSingle(), handle null
const { data: settings, error: err } = await supabase
  .from('settings').select('*').eq('user_id', user.id).maybeSingle()
if (!settings) { /* create defaults */ }

.maybeSingle() returns data: null with no error for 0 rows. For 2+ rows, supabase-js raises PGRST116 itself, with details like Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row (from the postgrest-js source).

Prompt for Cursor, Lovable, Bolt or Claude Code

My Supabase query using .single() fails with PGRST116 ("JSON object requested,
multiple (or no) rows returned"). Do not loosen RLS and do not add USING (true).
1. Log error.details to see whether it is 0 rows or 2+ rows.
2. If 0 rows: check that the request carries the signed-in user's session, and show me
   the SELECT policies on the table. Write a policy scoped to (select auth.uid()).
3. If 2+ rows: filter on a unique column and propose a unique constraint.
4. Only switch to .maybeSingle() where having no row is a valid state, and handle null.
Show me the SQL before running it.

Related: 42501 “permission denied for table” (no grant at all) and “new row violates row-level security policy” (a write blocked by a policy). Getting rows back but exactly 1,000 of them? That’s the Data API’s row cap: Supabase only returns 1,000 rows.

When to get help

Get help if a row is visible in the dashboard but your app never sees it and you can't find the policy, if your AI tool keeps 'fixing' this by loosening RLS, or if duplicate rows mean data integrity problems elsewhere.

FAQ

What does "JSON object requested, multiple (or no) rows returned" mean?

It is PostgREST error PGRST116 (HTTP 406). PostgREST's error reference describes it as more than 1 or no items returned when requesting a singular response. supabase-js's .single() requests a singular response, so any result that is not exactly one row fails. Newer PostgREST versions word the same error "Cannot coerce the result to a single JSON object", with details such as "The result contains 0 rows".

The row is in the Table Editor. Why does .single() say no rows?

Because the dashboard and SQL editor do not run as your app's user. On Supabase they run as a privileged role that bypasses Row Level Security. Your app runs as anon or authenticated, and with RLS enabled a row no policy allows is silently filtered out, so the count is 0. Supabase's RLS docs note that once RLS is enabled, no data is accessible through the API with the public key until you create policies.

Should I just switch to .maybeSingle()?

Only if no row is a valid state. .maybeSingle() returns data: null and no error for 0 rows, which also hides an RLS bug if the row should exist. If every user is supposed to have the row (like a profile), fix the policy or create the row on signup instead.

Why does .maybeSingle() still throw PGRST116?

If 2 or more rows match, supabase-js raises PGRST116 itself with the message "JSON object requested, multiple (or no) rows returned" and details like "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row". Make the filter unique.

Is this the same as "permission denied for table" (42501)?

No. 42501 means the role has no GRANT on the table at all, so the query is rejected outright. PGRST116 means the query ran and returned the wrong number of rows. RLS filtering produces PGRST116 (0 rows), not 42501.

Sources

Related fixes