我在supabase中有这两个表(#1 + #2)和一个关系表(#3),其中用户可以与许多客户端一起工作,并且客户端可能有许多用户。
2.- 配置文件(创建授权用户后自动生成,其中 user_id 与 auth.uid() 相同):
3.- rel_clients_profiles 加入上表:
然后我(在 NextJS 中)获取与特定用户相关的客户端,如下所示:
现在我的用户很少,实际上按预期工作,但我想知道是否有更好的方法来获取相关客户端?
感谢对此的任何评论,因为我发现了如何从多对多连接表中获取数据,但关于如何仅过滤相关项目的示例很少。
使用“内部联接”是我为此找到的解决方案。如果它对任何人有帮助,这就是在这种情况下它的工作原理:
获取客户:
const { data, error } = await supabase
.from('clients')
.select('id, client_name, profiles(user_id, email)')
.filter('profiles.user_id', 'eq', usID)
客户表上的 RLS 政策:
create policy "Only Client members can view its own client details."
on clients
for select using (
auth.uid() in (
select profile_id from rel_profiles_clients
where client_id = clients.id
)
);