Skip to content

Why is my select returning an empty data array and I have data in the table?

通常这意味着你启用了RLS(行级安全性)但没有策略,或者不符合策略。也可能意味着你设置了过滤器,但没有行符合条件。

🌐 Usually this means you have RLS (row level security) enabled and no policy, or do not meet the policy. It can also mean you have a filter and have no rows matching that.

如果你启用了 RLS,你可以通过在表上禁用 RLS 来进行测试。如果你的查询可以正常工作,那么说明你没有相关策略或者不符合这些策略。

🌐 If you have RLS enabled you can test by disabling RLS on the table. If your query works then you have no policies or do not meet them.

如果你的策略依赖于有用户登录(设置为 authenticated 或使用 auth.uid()),那么你可以通过将 TO 设置为 anon 并将你的策略设置为 TRUE 来检查。如果这样可行,那就说明你在调用时没有已登录的用户(授权头里没有 JWT)。

🌐 If you have policies that depend on having a signed in user (TO set to authenticated or using auth.uid()) then you can check by setting TO as anon and setting your policy to TRUE. If that works then you don't have a signed in user (with a JWT in the authorization header) when you make the call.

想了解更多关于 RLS 的信息,请访问 https://supabase.com/docs/guides/auth/row-level-security

🌐 For more information on RLS see https://supabase.com/docs/guides/auth/row-level-security

补充:要测试你在调用时是否有用户会话,你可以在 SQL 编辑器中添加这个函数,并在代码中与当前的数据库调用同一位置进行 RPC 调用。

🌐 Adding: To test if you have a user session at time of your call you can add this function with the SQL editor and an RPC call in your code at same place as your current database call.

1
create function test_authorization_header() returns json
2
language SQL
3
as
4
$$
5
select auth.jwt();
6
$$;
1
const { data: testData, error: testError } = await supabase.rpc('test_authorization_header')
2
console.log(`The user role is ${testData.role} and the user UUID is ${testData.sub}. `, testError)

如果你的角色是 anon 或 service_role,那么在你调用时你是没有用户会话的。

🌐 If you have anon or service_role as the role then you do not have a user session at the time of your call.

要删除测试 SQL 函数:

🌐 To delete the test SQL function:

1
drop function test_authorization_header;