Supabase PGRST116 "JSON object requested, multiple (or no) rows returned": what .single() is telling you

By AI App Medic · Updated · Supabase · Database · RLS · Lovable

A page that loads one record, such as a profile, a settings row or a single order, fails and the console or Network tab shows a 406 with a body like one of these:

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

{
  "code": "PGRST116",
  "details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row",
  "hint": null,
  "message": "JSON object requested, multiple (or no) rows returned"
}

Both are the same error. Older PostgREST versions used the second message for everything, current Supabase projects send the first one from the server, and supabase-js produces the second one itself when .maybeSingle() gets more than one row. That is why searching for one message turns up answers about the other.

The cause is always the same: the code called .single() or .maybeSingle(), which promise exactly one row, and the database returned a different number. AI builders write .single() on almost every lookup by default, so this shows up a lot in Lovable, Bolt and Cursor apps, typically right after sign up, on a fresh account, or after a data import.

Step 1: read the details, not the message

The message is vague on purpose. The details field tells you which of two unrelated problems you have. Log the full error where the query runs:

const { data, error } = await supabase
  .from("profiles")
  .select("*")
  .eq("id", user.id)
  .single();

if (error) {
  console.error(error.code, error.details, error.message);
}

If it says 0 rows, go to Step 2. If it says 2 rows or more, go to Step 4. If your app shows a generic "Something went wrong" toast, the real error is often swallowed by a try/catch the AI generated. Open the Network tab, find the red request to /rest/v1/ and read the response body there.

Step 2: zero rows, find out whether the row exists

Open the SQL editor in Supabase and look the row up with the same filter the app uses:

select * from public.profiles where id = 'PASTE-THE-USER-ID';

Copy the id from the failing request URL in the Network tab, for example ?id=eq.3f6c..., so you test the value the app really sent. Then:

Step 3: the row exists but the app gets zero rows

The SQL editor and the Table Editor run as a role that skips row level security. Your app runs as anon when nobody is signed in and as authenticated after login. RLS removes the rows a role may not see before the result is counted, so a row hidden by a policy looks exactly like a row that does not exist.

List the policies on the table:

select policyname, cmd, roles, qual
from pg_policies
where schemaname = 'public' and tablename = 'profiles';

If there is no policy with cmd = SELECT (or ALL) that matches this user, add one:

create policy "Users can read own profile"
  on public.profiles for select to authenticated
  using ((select auth.uid()) = id);

If the policy is fine, check the session. A query that runs before supabase-js has restored the session from storage goes out as anon, and auth.uid() is null, so the policy matches nothing. In the Network tab, open the request headers: if Authorization is missing or carries the same key as apikey instead of a long user token, the request was anonymous. Load user data only once the session is known:

useEffect(() => {
  const { data: sub } = supabase.auth.onAuthStateChange((_event, session) => {
    if (session?.user) loadProfile(session.user.id);
  });
  return () => sub.subscription.unsubscribe();
}, []);

Do not fix this by using the service role key in the browser or by adding using (true). Both make the error go away by making the data readable to everyone. The policy patterns for each operation are in Lovable + Supabase RLS not working.

Updates and deletes that return PGRST116

The same error appears on writes that end in .select().single():

const { data, error } = await supabase
  .from("projects")
  .update({ name })
  .eq("id", projectId)
  .select()
  .single();

If the filter matched no row this user may update, nothing changes and the result has zero rows. Updating needs an UPDATE policy, and returning the row needs a SELECT policy as well. Check both. An insert is different: an insert blocked by RLS fails with 42501 instead, which is covered in new row violates row-level security policy.

Step 4: two or more rows, the data has duplicates

When details says 2 rows or more, the query is fine and the data is not what the code assumes. Typical examples are two profile rows for one user, two settings rows, two subscriptions for one customer, or a slug used twice. They usually come from a form submitted twice, a retry, or an import. Find them:

select user_id, count(*)
from public.user_settings
group by user_id
having count(*) > 1;

Decide which row to keep, delete the others, then make the database refuse the duplicate from now on:

alter table public.user_settings
  add constraint user_settings_user_id_key unique (user_id);

With the constraint in place, a second insert fails with code 23505 instead of silently creating a second row. For "create it if it does not exist" logic, use an upsert on that column instead of a read followed by an insert, which is a race when two requests arrive together:

const { data, error } = await supabase
  .from("user_settings")
  .upsert({ user_id: user.id, theme: "dark" }, { onConflict: "user_id" })
  .select()
  .single();

An upsert needs INSERT and UPDATE policies on the table, plus SELECT for the .select().

If several rows are genuinely allowed, for example many orders per user and you want the latest, say so in the query rather than relying on luck:

const { data, error } = await supabase
  .from("orders")
  .select("*")
  .eq("user_id", user.id)
  .order("created_at", { ascending: false })
  .limit(1)
  .maybeSingle();

Step 5: pick the right method for each query

Swapping every .single() for .maybeSingle() is not a fix by itself. If the row is hidden by RLS, the error turns into a silent null and the page shows an empty state that is just as wrong. Use .maybeSingle() where zero rows is expected, and fix the policy where it is not.

Prevention

Give every column the code treats as unique a unique constraint or primary key, especially user_id on one-per-user tables. Create per-user rows in the database with a trigger instead of from the browser. Test new accounts, signed-out visitors and a second user before you ship, because the dashboard bypasses RLS and hides this class of bug. When an AI prompt "fixes" PGRST116, read the diff: a new .limit(1), a broad policy or a service role key in client code means the cause is still there.

FAQ

What does PGRST116 mean in Supabase?

It means the request asked for exactly one row and the result was not one row. You get it from .single() when the query returns zero rows or more than one, and from .maybeSingle() when it returns more than one. The HTTP status is 406 Not Acceptable.

What is the difference between .single() and .maybeSingle()?

Both return one object instead of an array. .single() fails with PGRST116 when zero or several rows match. .maybeSingle() returns data null with no error when zero rows match, and still fails when several match.

Why does Supabase say 406 Not Acceptable?

.single() sends the header Accept: application/vnd.pgrst.object+json, which asks PostgREST for one object. When the result is not exactly one row, PostgREST cannot produce that format and answers 406. It is not a permissions error and not a CORS problem.

The row is in my table, so why does .single() find zero rows?

Almost always row level security. The dashboard runs as a role that bypasses RLS, while your app runs as anon or authenticated. Without a SELECT policy that matches this user, the row is invisible to the app and the query counts zero rows.

Should I just add .limit(1) to stop the error?

Only when several rows are genuinely allowed and you sort first, for example the newest order. If the column is supposed to be unique, .limit(1) hides duplicate data and can show a user the wrong record. Fix the duplicates and add a unique constraint instead.

Why did my update with .single() fail and change nothing?

An update with .select().single() returns PGRST116 when the filter matched no row the user is allowed to update, which usually means a missing UPDATE or SELECT policy or a wrong id. Check the policies for that table and the value in the eq() filter.

Related guides