"Uncaught Error: More than one relationship was found" with Supabase client query

"Uncaught Error: More than one relationship was found" with Supabase client query

我正在构建一个多租户应用程序,但在添加指向相同 table 的多个关系后 运行 出错了:

Uncaught Error: More than one relationship was found for teams and users

执行此查询时:

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

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

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
)

在幕后,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'
    }
  ]
}

稍微挖掘一下deeper into the PostgREST docs,原来我要找的是消歧运算符,!

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

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