Supabase "JSON object requested, multiple (or no) rows returned" (PGRST116): how to fix it
Your Lovable, Bolt or Cursor app calls .single() and the console shows JSON object requested, multiple (or no) rows returned with code PGRST116 (HTTP 406). PostgREST promised exactly one JSON object. Your query returned zero rows or more than one. The code does not tell you which, so guessing "no row, so insert" is how apps create duplicate bookings and profiles.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Know what PGRST116 actually means
.single() asks for a singular response. Zero rows and two-or-more rows both fail with the same PGRST116. Nothing in the error object says which case you hit. Treat it as "not exactly one row", not as "missing".
// This fails on 0 rows AND on 2+ rows
const { data, error } = await supabase
.from('profiles')
.select('*')
.eq('id', userId)
.single()
2. Use .maybeSingle() when zero rows is normal
Looking up a profile that may not exist yet? Use .maybeSingle(). Zero rows becomes data: null with no error. It still errors if more than one row matches, which is what you want for a unique lookup.
const { data: profile, error } = await supabase
.from('profiles')
.select('*')
.eq('id', user.id)
.maybeSingle()
if (error) throw error
if (!profile) {
// create the profile, or show onboarding
}
If zero rows is not legitimate (you know the row should exist), do not paper over it with .maybeSingle(). That usually means RLS hid the row: see empty data with no error and new row violates RLS.
3. Zero rows often means RLS, not a missing insert
RLS is applied before the row count. A profile you can see in the Table Editor as the service role can still look like "zero rows" to the logged-in user. Confirm with a second account and a policy that uses (select auth.uid()). Do not switch to the service role key in the browser to "fix" it.
4. More than one row means a missing unique constraint
If a lookup by email, slug or (user_id, date) returns two rows, the database allows duplicates your code assumed were impossible. Adding .limit(1) hides the bug and can serve the wrong row. Fix the database:
-- after de-duplicating existing rows
alter table public.bookings
add constraint bookings_user_day_unique
unique (user_id, booking_date);
For check-then-insert races, handle Postgres 23505 (unique violation) as "someone else just created it" and fall through to an update. Do not treat PGRST116 alone as "insert a new row" — that is how one double-tap becomes thousands of duplicates.
const { data: existing, error } = await supabase
.from('bookings')
.select('id')
.eq('user_id', userId)
.eq('booking_date', date)
.maybeSingle()
if (error) throw error
if (existing) {
await supabase.from('bookings').update({ response }).eq('id', existing.id)
} else {
const { error: insertError } = await supabase.from('bookings').insert({ /* … */ })
if (insertError?.code === '23505') {
await supabase.from('bookings').update({ response })
.eq('user_id', userId).eq('booking_date', date)
} else if (insertError) throw insertError
}
5. Prefer .maybeSingle() + a unique constraint over .single()
Keep .single() only when the row must exist and must be unique (and you want a hard failure otherwise). For "get or create" flows in Lovable / Bolt / Cursor apps, .maybeSingle() plus a unique constraint is the safe default.
Quick checklist
- Read
PGRST116as "not exactly one row", not "missing" - Zero-is-ok lookups use
.maybeSingle() - Zero-should-exist lookups: check RLS before changing code
- Two-or-more: add a unique constraint, then de-dupe
- Never branch on
PGRST116alone to insert
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.