Supabase PGRST116 “Cannot coerce the result to a single JSON object” – .single() explained

By Abheesht Tripathi · Published

{"code":"PGRST116","details":"The result contains 0 rows","hint":null,"message":"Cannot coerce the result to a single JSON object"}  (HTTP 406)

What it means

.single() tells the API “return exactly one row as an object, not an array”. If the query matches zero rows or more than one, the API refuses with PGRST116 and HTTP 406. The details field tells you which: The result contains 0 rows or The result contains 2 rows. Older versions said JSON object requested, multiple (or no) rows returned.

Zero rows: the usual causes

  1. The row really doesn't exist yet, for example a profile row that is created by a trigger after sign-up, or a settings row the user never saved.
  2. RLS hides it. Reads that fail a policy are not errors; the row is simply not returned. As postgres in the SQL Editor you see it, as the user you don't.
  3. An update or delete matched nothing. .update({...}).eq('id', id).select().single() returns PGRST116 when the UPDATE policy's using clause didn't match. Without .single() this fails silently: zero rows updated, no error.
  4. The filter value is wrong: an id from another environment, a number sent as a string with spaces, or an undefined variable.
  5. Server code without the user's session. A route handler that creates its own client with only the publishable key runs as anon, so “get my profile” matches nothing under RLS. The same query works in the browser, which is what makes this one confusing.

Two or more rows: the usual causes

Fix: use maybeSingle() when “not found” is normal

const { data, error } = await supabase
  .from('profiles')
  .select('*')
  .eq('id', userId)
  .maybeSingle();

if (error) throw error;      // a real problem, including more than one row
if (!data) {
  // not found, or not visible to this user under RLS
}

maybeSingle() returns data: null for zero rows. It still errors on two or more rows, which is what you want: that is a data bug, not an empty state.

Fix: make “one row” true in the database

If a column is supposed to identify one row, enforce it, so .single() can't see two:

alter table public.profiles add constraint profiles_username_key unique (username);

When you really want “the latest one”, say so explicitly: .order('created_at', { ascending: false }).limit(1).maybeSingle().

Find out whether RLS is the reason

-- as the table owner: does the row exist?
select id, user_id from public.profiles where id = '<id>';

-- as the user: can they see it?
begin;
set local role authenticated;
select set_config('request.jwt.claims',
  json_build_object('sub', '<user-uuid>', 'role', 'authenticated')::text, true);
select id from public.profiles where id = '<id>';
rollback;

A row in the first query and none in the second means a policy is hiding it. Fix the policy or the ownership column, not the client code.

How to verify

The call returns an object (or null with maybeSingle()) and error is null. Then try an id that belongs to another user: you should get null, never their row.

Related guides

More in Supabase RLS and Storage · all guides