DEV Community

Cover image for Why your Supabase query returns nothing, with no error (3 common causes)
Masaru Nishino
Masaru Nishino

Posted on AI-assisted

Why your Supabase query returns nothing, with no error (3 common causes)

"The data is in the table, but my app shows nothing."

This is one of the most common Supabase questions, especially in apps built with Lovable, Bolt or Cursor. And it's hard to search for, because there is no error. The query succeeds. It just returns [].

Almost every time, the cause is Row Level Security (RLS).

Why the dashboard looks fine

The SQL Editor in the Supabase dashboard runs as a database admin, so it skips RLS. Your app doesn't. It runs as anon (not signed in) or authenticated (signed in), and RLS decides which rows that user can see.

So the same query can return 5 rows in the dashboard and 0 rows in your app. Both are "correct".

To reproduce this, I wrote a small script against a local Supabase. It creates a notes table with 5 rows: 3 belong to client A, 2 belong to client B. Then it signs in as client A and runs the query three different ways.

In all three cases, the app got 0 rows and no error, while a direct Postgres count showed 5. After each fix, client A saw exactly their own 3 rows, and none of client B's.

Here are the three causes.

Cause 1: RLS is on, but there is no policy

alter table public.notes enable row level security;
-- ...and no policy was created
Enter fullscreen mode Exit fullscreen mode

When RLS is enabled and no policy allows select, Postgres returns nothing. It doesn't raise an error. It just filters out every row.

Fix: add a policy that says who can read which rows.

create policy "Users can read their own notes"
on public.notes
for select
to authenticated
using (owner_id = (select auth.uid()));
Enter fullscreen mode Exit fullscreen mode

Cause 2: The policy compares the wrong column

-- Looks right, but isn't
using (id = (select auth.uid()))
Enter fullscreen mode Exit fullscreen mode

id is the note's own ID. auth.uid() is the signed-in user's ID. They are never equal, so the policy matches no rows.

This one is common in AI-written policies, because id and user_id / owner_id look similar.

Fix: compare the column that stores the owner.

using (owner_id = (select auth.uid()))
Enter fullscreen mode Exit fullscreen mode

Cause 3: The query runs before sign-in

The policy is written for authenticated, but the query runs before the session is ready. At that moment the request is still anon, and no policy allows anon to read. Result: 0 rows.

This often happens when a page loads data on first render, before sign-in has finished, or when a server component uses a client that doesn't have the user's session.

Fix: make sure the user is signed in before you query.

const { data: { user } } = await supabase.auth.getUser()
if (!user) {
  // not signed in yet: redirect, or wait for the session
  return
}

const { data, error } = await supabase.from('notes').select('*')
Enter fullscreen mode Exit fullscreen mode

On the server (for example in Next.js), use a client that reads the user's session from cookies, so the request runs as that user.

Check your own app in one minute

Run the same query in the SQL Editor. If it returns rows there but not in your app, it's almost certainly RLS.

To see what a real user sees, you can test as that user inside a transaction:

begin;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"<user-id>"}', true);

select count(*) from public.notes;  -- what this user can see

rollback;
Enter fullscreen mode Exit fullscreen mode

Replace <user-id> with the user's ID from Authentication > Users. If the count is 0 here, the problem is your policy, not your frontend.

Two "fixes" to avoid

  • Turning RLS off. The list will show up, but now anyone with your anon key can read every row.
  • Using the service_role key in the browser. It skips RLS completely. Never ship it to the client.

Both make the empty list go away by removing the protection.

The script

The local script that reproduces all three causes (and fixes each one) is here. It only runs against localhost, creates a temporary table, and always drops it at the end:

https://github.com/kuros-works/client-portal-demo/blob/main/scripts/local-seed/README.md#demo-why-a-supabase-query-returns-nothing-no-error


I build Next.js + Supabase client portals in Tokyo. If you'd rather have someone find and fix this in your app, I do it as a small fixed-price service on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-or-permission-error-fixed-in-your-app-2126481891543141476

Top comments (0)