Supabase PGRST116 "Cannot coerce the result to a single JSON object": what it means and how to fix it
You call .single() in supabase-js and get this back: {"code":"PGRST116","message":"Cannot coerce the result to a single JSON object","details":"The result contains 0 rows","hint":null} Older PostgREST versions s
You call .single() in supabase-js and get this back:
{"code":"PGRST116","message":"Cannot coerce the result to a single JSON object","details":"The result contains 0 rows","hint":null}
Older PostgREST versions say "JSON object requested, multiple (or no) rows returned". The HTTP status is 406.
What it means
You asked for exactly one row. The query returned zero rows or more than one. PostgREST refuses to guess.
Why it happens
- The filter matched nothing: wrong id, the row was not created yet, or Row Level Security hides it from this user.
- The filter matched several rows because the column is not unique.
- You ran
.insert(...).select().single()but your SELECT policy does not let the user read the row they just inserted.
How to fix it
- If "no row" is a normal state, use
.maybeSingle()and handlenull:
const { data, error } = await supabase
.from('profiles')
.select('*')
.eq('id', user.id)
.maybeSingle()
if (!data) { /* create the profile or show onboarding */ }
- Filter on a primary key or another unique column.
- If the row is in the table but not returned, the problem is RLS. Add a SELECT policy scoped to the owner.
"But I can see the row in the dashboard"
The dashboard bypasses RLS. Your app's role does not. Test the query as the user, not as the admin.
The fix to avoid
Do not "solve" it by making the SELECT policy using (true). Then every user can read every row in that table. PGRST116 itself is not a security problem. That shortcut is.
Check it is fixed
Test as a brand-new user with no row and as an existing user. Neither should throw PGRST116.
Full reference with the same steps: https://proofforge.live/errors/pgrst116 . If you are not sure whether your tables are open to the public, run the free outside check at https://proofforge.live
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.