Supabase多对多连接问题与dart

8zzbczxx  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在尝试让上面提到的多对多连接特性按照下面的描述工作:https://supabase.com/docs/guides/api/joins-and-nesting#many-to-many-joins
我有三个简单的表格:

create table parties (
  id uuid primary key,
  name text not null
);

create table users (
  id uuid not null references auth.users on delete cascade,
  phone_number text,

  primary key (id)
);

create table parties_members (
  party_id uuid not null references parties on delete cascade,
  user_id uuid not null references users on delete cascade
);

alter table parties_members add primary key (party_id, user_id);

我现在尝试创建一个连接partiesusers的查询,但我得到一个:Could not find a relationship between 'parties' and 'users' in the schema cache, code: PGRST200

try {
  var parties = await supabase.from("parties").select('''
    id,
    parties_members(*)
  ''');

  var users = await supabase.from("users").select('''
    id,
    parties_members(*)
  ''');

  log("parties with join table: $parties");
  log("users with join table: $users");

  var partiesWithUsers = await supabase.from("parties").select('''
    id,
    users(*)
  ''');
} catch (e, stacktrace) {
  log("Error fetching parties", error: e, stackTrace: stacktrace);
}

前两个连接工作。。所以看起来来自任何一方的引用都被发现了,它只是不能使用连接表进行跳转。

[log] parties with join table: [{id: b6e10467-7d1f-4705-8bb5-b59484c86955, parties_members: [{party_id: b6e10467-7d1f-4705-8bb5-b59484c86955, user_id: 48d4a132-46f1-439a-94b3-3ecb49d98a49}]}]
[log] users with join table: [{id: 48d4a132-46f1-439a-94b3-3ecb49d98a49, parties_members: {party_id: b6e10467-7d1f-4705-8bb5-b59484c86955, user_id: 48d4a132-46f1-439a-94b3-3ecb49d98a49}}]

知道我哪里做错了吗
使用:supabase_flutter: ^1.8.1

euoag5mw

euoag5mw1#

您缺少parts_members表上的主键约束。您可以像这样创建一个复合主键来完成多对多关系的定义。

create table parties_members (
  party_id uuid not null references parties on delete cascade,
  user_id uuid not null references users on delete cascade,
  primary key(party_id, user_id)
);

相关问题