Supabase 客户端查询出现“未捕获错误:发现多个关系”

Pre*_*fix 4 postgresql relational-database supabase supabase-database

我正在构建一个多租户应用程序,并在添加指向同一个表的多个关系后遇到错误:

Uncaught Error: More than one relationship was found for teams and users
Run Code Online (Sandbox Code Playgroud)

执行此查询时:

const data = await supabaseClient.from('organizations')
.select(`
  *,
  teams(
    id,
    org_id,
    name,
    members:users(
      id,
      full_name,
      avatar_url
    )
  )
`);
Run Code Online (Sandbox Code Playgroud)

我有以下表结构(为简洁起见,省略了一些字段):

table users (
  id uuid PK
  full_name text
  email text
)

table organizations (
  id uuid PK
  ....
)

table organization_memberships (
  id uuid PK
  organization_id uuid FK
  user_id uuid FK
  role ENUM
)

table teams (
  id uuid PK
  name text PK
)

table team_memberships (
    id uuid PK
    team_id uuid FK
    user_id uuid FK
    role ENUM
)

table team_boards (
  id uuid PK
  team_id uuid FK
  owner_id uuid FK
)
Run Code Online (Sandbox Code Playgroud)

在底层,Supabase 使用 PostREST 进行查询。我从错误消息中解读出该查询不明确,并且不确定要满足哪种关系。我不确定如何告诉 Supabase 在这个特定查询中使用哪个关系来避免此错误。

这是来自 postREST 的更详细的控制台错误:

{
  hint: "By following the 'details' key, disambiguate the request by changing the url to /origin?select=relationship(*) or /origin?select=target!relationship(*)",
  message: 'More than one relationship was found for teams and users',
  details: [
    {
      origin: 'public.teams',
      relationship: 'public.team_memberships[team_memberships_team_id_fkey][team_memberships_user_id_fkey]',
      cardinality: 'm2m',
      target: 'public.users'
    },
    {
      origin: 'public.teams',
      relationship: 'public.team_boards[team_boards_team_id_fkey][team_boards_owner_id_fkey]',
      cardinality: 'm2m',
      target: 'public.users'
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Pre*_*fix 5

深入研究PostgREST 文档,发现我正在寻找的是消歧运算符!

工作查询如下所示(请注意,我们正在消除使用哪个关系来满足members查询的歧义):

const data = await supabaseClient.from('organizations')
.select(`
  *,
  teams(
    id,
    org_id,
    name,
    members:users!team_memberships(
      id,
      full_name,
      avatar_url
    )
  )
`);
Run Code Online (Sandbox Code Playgroud)