过滤Supabase与Join Table的多对多关系

问题描述 投票:0回答:1

我在supabase中有这两个表(#1 + #2)和一个关系表(#3),其中用户可以与许多客户端一起工作,并且客户端可能有许多用户。

1.- 客户:

2.- 配置文件(创建授权用户后自动生成,其中 user_id 与 auth.uid() 相同):

3.- rel_clients_profiles 加入上表:

这是我的客户表,仅 RLS 策略:

这是我的公共架构。

然后我(在 NextJS 中)获取与特定用户相关的客户端,如下所示:

现在我的用户很少,实际上按预期工作,但我想知道是否有更好的方法来获取相关客户端?

感谢对此的任何评论,因为我发现了如何从多对多连接表中获取数据,但关于如何仅过滤相关项目的示例很少。

next.js many-to-many supabase
1个回答
0
投票

使用“内部联接”是我为此找到的解决方案。如果它对任何人有帮助,这就是在这种情况下它的工作原理:

获取客户:

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

)

);
© www.soinside.com 2019 - 2024. All rights reserved.