在Supabase中查询子子表 / 在JOIN查询中的孙子关系

huangapple go评论71阅读模式
英文:

Querying sub-sub table in Supabase / Grandchild relationship in JOIN QUERY

问题

给定以下架构:

CREATE TABLE organization (
   org_name text NOT NULL,
   PRIMARY KEY (org_name)
);

CREATE TABLE teams (
   org_name text NOT NULL,
   team_name text NOT NULL,
   PRIMARY KEY (org_name, team_name),
   FOREIGN KEY (org_name)
      REFERENCES organization (org_name)
);

CREATE TABLE projects (
   org_name text NOT NULL,
   team_name text NOT NULL,
   project_name text NOT NULL,
   products jsonb,
   PRIMARY KEY (org_name, team_name, project_name),
   FOREIGN KEY (org_name)
      REFERENCES organization (org_name),
   FOREIGN KEY (org_name, team_name)
      REFERENCES teams (org_name, team_name)
);

我想查询 teams 表,同时返回 projects 表中的 products。是否有方法可以实现这个需求?

英文:

Given the following schema:

<img src="https://i.stack.imgur.com/NrfjU.png" width="300" />

CREATE TABLE organization (
   org_name text  NOT NULL,
   PRIMARY KEY (org_name)
);

CREATE TABLE teams (
   org_name text NOT NULL,
   team_name text NOT NULL,
   PRIMARY KEY (org_name, team_name),
   FOREIGN KEY (org_name)
      REFERENCES organization (org_name)
);

CREATE TABLE projects (
   org_name text NOT NULL,
   team_name text NOT NULL,
   project_name text NOT NULL,
   products jsonb,
   PRIMARY KEY (org_name, team_name, project_name),
   FOREIGN KEY (org_name)
      REFERENCES organization (org_name),
   FOREIGN KEY (org_name, team_name)
      REFERENCES teams (org_name, team_name)
);

I want to query the teams table but also return the products from the projects table. Is there a way to do this?

答案1

得分: 1

以下是翻译好的部分:

对于单一关系设置,您可以执行以下操作(假设为自上而下的关系:

organization -&gt; teamsteams -&gt; projects

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
  .from('organization')
  .select(`*, 
           teams(*,
                 projects(products)
                )
          `);
console.log(JSON.stringify(ret));

在这种情况下,这是不可能的,您将收到以下错误消息:

> 无法嵌入,因为找到了超过一个关系 'organization' 和 'teams'。

在这种情况下,您可以在调用 supabase 时选择关系,要么:

teams!projectsteams!teams_org_name_fkey。前者在大多数情况下更为推荐。

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
  .from('organization')
  .select(`*, 
          teams!projects(*,
                        projects(products)
                        )
         `);

输出:

> [{"org_name":"Contoso","teams":[{"org_name":"Contoso","team_name":"Contoso Café","projects":[{"products":{"Dairy":"latte","coffee":["french press","expresso","cold brew"]}}]}]}

英文:

For single relationships set, then you can do the following (assuming a top-bottom relationship as:

organization -&gt; teams and teams -&gt; projects

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
  .from(&#39;organization&#39;)
  .select(`*, 
           teams(*,
                 projects(products)
                )
          `);
console.log(JSON.stringify(ret));

In this case, it would not be possible and you would get the following error:
> Could not embed because more than one relationship was found for 'organization' and 'teams'

In this case, you can pick the relationship when calling supabase with either:
teams!projects or teams!teams_org_name_fkey. The former is preferred for most cases.

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
  .from(&#39;organization&#39;)
  .select(`*, 
          teams!projects(*,
                        projects(products)
                        )
         `);

Output:

> [{"org_name":"Contoso","teams":[{"org_name":"Contoso","team_name":"Contoso Café","projects":[{"products":{"Dairy":"latte","coffee":["french press","expresso","cold brew"]}}]}]}]

huangapple
  • 本文由 发表于 2023年2月9日 00:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定