🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Database / Supabase RLS

Supabase .single() error PGRST116: JSON object requested, multiple (or no) rows returned

LovableBoltCursorReplitv0

Published

The error

PGRST116 (HTTP 406): 'JSON object requested, multiple (or no) rows returned' / newer wording 'Cannot coerce the result to a single JSON object' with details 'The result contains 0 rows' (or 2+ rows) / page crashes or shows blank after .single()

Why it happens

When your code calls .single(), supabase-js asks PostgREST (the API layer in front of your Supabase database) for one JSON object instead of an array. PostgREST only returns that if the query matched exactly one row; with 0 rows or 2+ rows it responds with HTTP 406 and error code PGRST116. Without .single() the same query would just return [] or a list with no error. In vibe-coded apps the 0-row case is by far the most common, and the row usually does exist: Row Level Security hides rows the current user is not allowed to see, so they are silently removed before the count. Typical triggers are a query that runs before the auth session has loaded (auth.uid() is null, so a policy like auth.uid() = user_id matches nothing), a server-side or Edge Function client that does not forward the user's JWT, a missing SELECT policy, a profile row that was never created by the signup trigger, or filtering on the wrong column (id vs user_id). The 2+ rows case happens when the column you filter on is not unique, for example duplicate profiles rows created by a retry or a double-click. Writes are a special case: update().eq().select().single() or delete().select().single() return PGRST116 when RLS (USING or the SELECT policy) filters the target row out, because 0 rows were changed. insert().select().single() is different: if the new row is not visible to the SELECT policy, Postgres throws 42501 'new row violates row-level security policy' instead, because rows returned by RETURNING are never silently dropped.

How to fix it

  1. Read the error details, not just the message. 'The result contains 0 rows' (or 'Results contain 0 rows') means nothing matched; '2 rows' or more means your filter is not unique. The code is always PGRST116 with HTTP 406, whichever wording your PostgREST version uses.
  2. Decide whether zero rows is a valid answer. For lookups that may legitimately find nothing (a profile not created yet, an optional setting, 'does this slug exist?'), replace .single() with .maybeSingle(). It returns data: null and error: null for 0 rows, so handle null explicitly (show a 404, an empty state, or create the row). Keep .single() only where exactly one row must exist.
  3. If the row exists in the Table Editor but the app gets 0 rows, check RLS. The Dashboard SQL editor runs as postgres and bypasses policies, so test as the user instead: confirm RLS is on, that there is a SELECT policy for the authenticated role (for example using ((select auth.uid()) = user_id)), and that the user_id stored on the row really matches the signed-in user's id (same type, uuid vs text).
  4. Make sure the request is actually signed in. Wait for the session before querying (use supabase.auth.getSession() / onAuthStateChange on the client, or the @supabase/ssr server client that reads auth cookies in Next.js). In Edge Functions or API routes create the client with the caller's Authorization header. If the request is anonymous, auth.uid() is null and owner policies match nothing.
  5. Check the filter itself: right table, right column (profiles.id vs profiles.user_id), right value (not undefined, not a stale id from the URL). Log the exact value you pass to .eq() and run the same select in the SQL editor.
  6. If you see 2+ rows, fix the data and the schema, not just the query. Find duplicates (group by the column having count(*) > 1), delete or merge the extras, then add a unique constraint or unique index on that column (for profiles, make id the primary key referencing auth.users). Only use .limit(1).single() with an explicit .order() when 'pick the newest' is genuinely what you want.
  7. For update/delete with .select().single(): a PGRST116 here means 0 rows were updated or deleted, usually because the UPDATE/DELETE policy USING clause or the missing SELECT policy filtered the row out. Postgres needs a SELECT policy too for UPDATE to see the row. Add or fix those policies (see the related 'update saved but gone after refresh' fix), or use .maybeSingle() and treat null as 'not allowed or not found'.
  8. For insert().select().single(): this does not give PGRST116 for RLS. If the row is blocked by the INSERT policy or not visible to the SELECT policy you get 42501 'new row violates row-level security policy'. Fix the INSERT WITH CHECK and add a SELECT policy that lets the owner read the new row, or drop .select() if you do not need the row back. Upsert with ignoreDuplicates: true can return 0 rows when the row already existed, so use .maybeSingle() there.
  9. Never 'fix' PGRST116 by switching to the service_role key in the browser or disabling RLS. That hides the symptom and exposes every row. Fix the policy, the session, or the query.
  10. Retest as a brand-new user, an existing user, and a signed-out visitor. Each should get either the one row, a handled null, or a clean 'not found' state, with no 406 in the browser Network tab.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

My Supabase query fails with PGRST116 'JSON object requested, multiple (or no) rows returned' (or 'Cannot coerce the result to a single JSON object'). The details say: <PASTE details, e.g. The result contains 0 rows>. The query is: <PASTE the supabase.from(...)...single() call and the file it is in>. Do not disable RLS and do not use the service_role key in client code. 1) Tell me whether this is 0 rows or 2+ rows. 2) If zero rows can be valid here, switch to .maybeSingle() and handle data === null with a proper empty or not-found state. 3) If the row should exist, check that the query only runs after the auth session has loaded, that server and Edge Function clients forward the user's JWT, that the filter uses the right column and value, and show me the SQL for a SELECT policy like using ((select auth.uid()) = user_id) to authenticated. 4) If it is 2+ rows, give me SQL to find and remove duplicates and add a unique constraint. 5) If the call is update/delete/insert().select().single(), check the matching UPDATE/DELETE/INSERT policies and the SELECT policy. Explain each change in one line.

Error codes this fixes

FAQ

What does PGRST116 mean in Supabase?

It means you called .single() (which asks for exactly one JSON object) and the query matched 0 rows or more than 1 row. PostgREST returns HTTP 406 with code PGRST116. The details field tells you which: 'contains 0 rows' or '2 rows' and so on.

Should I use .single() or .maybeSingle()?

Use .maybeSingle() when finding nothing is a normal outcome; it returns data: null with no error for 0 rows (but still errors on 2+ rows). Use .single() only when exactly one row must exist and a missing row is a real bug.

The row is in the Table Editor, so why does .single() find 0 rows?

The Table Editor and SQL editor bypass Row Level Security. Your app runs as the anon or authenticated role, so RLS hides rows the current user cannot read. Check the SELECT policy, that the user is signed in when the query runs, and that user_id matches auth.uid().

Why did my message change to 'Cannot coerce the result to a single JSON object'?

Newer PostgREST versions reworded the message. The code (PGRST116) and the HTTP status (406) are the same, so match on the code, not the text.

Why does insert().select().single() give a row-level security error instead?

Postgres requires rows returned by INSERT ... RETURNING to pass the SELECT policy and throws 42501 rather than dropping them silently. Add a SELECT policy for the owner or remove .select() if you do not need the inserted row back.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.