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

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

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

问题

给定以下架构:

  1. CREATE TABLE organization (
  2. org_name text NOT NULL,
  3. PRIMARY KEY (org_name)
  4. );
  5. CREATE TABLE teams (
  6. org_name text NOT NULL,
  7. team_name text NOT NULL,
  8. PRIMARY KEY (org_name, team_name),
  9. FOREIGN KEY (org_name)
  10. REFERENCES organization (org_name)
  11. );
  12. CREATE TABLE projects (
  13. org_name text NOT NULL,
  14. team_name text NOT NULL,
  15. project_name text NOT NULL,
  16. products jsonb,
  17. PRIMARY KEY (org_name, team_name, project_name),
  18. FOREIGN KEY (org_name)
  19. REFERENCES organization (org_name),
  20. FOREIGN KEY (org_name, team_name)
  21. REFERENCES teams (org_name, team_name)
  22. );

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

英文:

Given the following schema:

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

  1. CREATE TABLE organization (
  2. org_name text NOT NULL,
  3. PRIMARY KEY (org_name)
  4. );
  5. CREATE TABLE teams (
  6. org_name text NOT NULL,
  7. team_name text NOT NULL,
  8. PRIMARY KEY (org_name, team_name),
  9. FOREIGN KEY (org_name)
  10. REFERENCES organization (org_name)
  11. );
  12. CREATE TABLE projects (
  13. org_name text NOT NULL,
  14. team_name text NOT NULL,
  15. project_name text NOT NULL,
  16. products jsonb,
  17. PRIMARY KEY (org_name, team_name, project_name),
  18. FOREIGN KEY (org_name)
  19. REFERENCES organization (org_name),
  20. FOREIGN KEY (org_name, team_name)
  21. REFERENCES teams (org_name, team_name)
  22. );

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

  1. const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
  2. const { data: ret, error } = await supabase
  3. .from('organization')
  4. .select(`*,
  5. teams(*,
  6. projects(products)
  7. )
  8. `);
  9. console.log(JSON.stringify(ret));

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

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

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

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

  1. const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
  2. const { data: ret, error } = await supabase
  3. .from('organization')
  4. .select(`*,
  5. teams!projects(*,
  6. projects(products)
  7. )
  8. `);

输出:

> [{"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

  1. const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
  2. const { data: ret, error } = await supabase
  3. .from(&#39;organization&#39;)
  4. .select(`*,
  5. teams(*,
  6. projects(products)
  7. )
  8. `);
  9. 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.

  1. const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
  2. const { data: ret, error } = await supabase
  3. .from(&#39;organization&#39;)
  4. .select(`*,
  5. teams!projects(*,
  6. projects(products)
  7. )
  8. `);

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:

确定