Supabase 查詢無錯誤卻回傳空結果的 3 個常見原因
Why your Supabase query returns nothing, with no error (3 common causes)
Supabase 查詢成功但回傳空結果,常見原因是 Row Level Security(RLS)未放行資料,而非 SQL 查詢報錯。作者以本地 Supabase 的 5 筆 notes 資料重現三種成因:沒有 select policy、政策比較錯誤欄位,以及查詢早於登入完成;修正後,client A 只看到自己的 3 筆資料。
"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
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()));
Cause 2: The policy compares the wrong column
-- Looks right, but isn't
using (id = (select auth.uid()))
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()))
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('*')
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;
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_rolekey 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:
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
來源:DEV Community · dev.to