Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: Add Query nested foreign tables throught a join table example for supabase-js (#29387)

Samuel Senga30e0ea4bff2
diff --git a/apps/docs/spec/examples/examples.yml b/apps/docs/spec/examples/examples.yml
index ce9992d257d..9a35a086b5e 100644
--- a/apps/docs/spec/examples/examples.yml
+++ b/apps/docs/spec/examples/examples.yml
@@ -1067,6 +1067,112 @@ functions:
</TabPanel>
</Tabs>
hideCodeBlock: true
+ - id: query-nested-foreign-tables-through-a-join-table
+ name: Query nested foreign tables through a join table
+ code: |
+ ```ts
+ const { data, error } = await supabase
+ .from('games')
+ .select(`
+ game_id:id,
+ away_team:teams!games_away_team_fkey (
+ users (
+ id,
+ name
+ )
+ )
+ `)
+ ```
+ data:
+ sql: |
+ ```sql
+ create table
+ users (
+ id int8 primary key,
+ name text
+ );
+ create table
+ teams (
+ id int8 primary key,
+ name text
+ );
+ -- join table
+ create table
+ users_teams (
+ user_id int8 not null references users,
+ team_id int8 not null references teams,
+
+ primary key (user_id, team_id)
+ );
+ create table
+ games (
+ id int8 primary key,
+ home_team int8 not null references teams,
+ away_team int8 not null references teams,
+ name text
+ );
+
+ insert into users (id, name)
+ values
+ (1, 'Kiran'),
+ (2, 'Evan');
+ insert into
+ teams (id, name)
+ values
+ (1, 'Green'),
+ (2, 'Blue');
+ insert into
+ users_teams (user_id, team_id)
+ values
+ (1, 1),
+ (1, 2),
+ (2, 2);
+ insert into
+ games (id, home_team, away_team, name)
+ values
+ (1, 1, 2, 'Green vs Blue'),
+ (2, 2, 1, 'Blue vs Green');
+ ```
+ response: |
+ ```json
+ {
+ "data": [
+ {
+ "game_id": 1,
+ "away_team": {
+ "users": [
+ {
+ "id": 1,
+ "name": "Kiran"
+ },
+ {
+ "id": 2,
+ "name": "Evan"
+ }
+ ]
+ }
+ },
+ {
+ "game_id": 2,
+ "away_team": {
+ "users": [
+ {
+ "id": 1,
+ "name": "Kiran"
+ }
+ ]
+ }
+ }
+ ],
+ "status": 200,
+ "statusText": "OK"
+ }
+ ```
+ description: |
+ You can use the result of a joined table to gather data in
+ another foreign table. With multiple references to the same foreign
+ table you must specify the column on which to conduct the join.
+ hideCodeBlock: true
- id: filtering-through-foreign-tables
name: Filtering through referenced tables
description: |
diff --git a/apps/docs/spec/supabase_js_v2.yml b/apps/docs/spec/supabase_js_v2.yml
index 870b0ffb972..9782e234594 100644
--- a/apps/docs/spec/supabase_js_v2.yml
+++ b/apps/docs/spec/supabase_js_v2.yml
@@ -3378,6 +3378,112 @@ functions:
joined column to identify which join to use. You can also give each
column an alias.
hideCodeBlock: true
+ - id: query-nested-foreign-tables-through-a-join-table
+ name: Query nested foreign tables through a join table
+ code: |
+ ```ts
+ const { data, error } = await supabase
+ .from('games')
+ .select(`
+ game_id:id,
+ away_team:teams!games_away_team_fkey (
+ users (
+ id,
+ name
+ )
+ )
+ `)
+ ```
+ data:
+ sql: |
+ ```sql
+ create table
+ users (
+ id int8 primary key,
+ name text
+ );
+ create table
+ teams (
+ id int8 primary key,
+ name text
+ );
+ -- join table
+ create table
+ users_teams (
+ user_id int8 not null references users,
+ team_id int8 not null references teams,
+
+ primary key (user_id, team_id)
+ );
+ create table
+ games (
+ id int8 primary key,
+ home_team int8 not null references teams,
+ away_team int8 not null references teams,
+ name text
+ );
+
+ insert into users (id, name)
+ values
+ (1, 'Kiran'),
+ (2, 'Evan');
+ insert into
+ teams (id, name)
+ values
+ (1, 'Green'),
+ (2, 'Blue');
+ insert into
+ users_teams (user_id, team_id)
+ values
+ (1, 1),
+ (1, 2),
+ (2, 2);
+ insert into
+ games (id, home_team, away_team, name)
+ values
+ (1, 1, 2, 'Green vs Blue'),
+ (2, 2, 1, 'Blue vs Green');
+ ```
+ response: |
+ ```json
+ {
+ "data": [
+ {
+ "game_id": 1,
+ "away_team": {
+ "users": [
+ {
+ "id": 1,
+ "name": "Kiran"
+ },
+ {
+ "id": 2,
+ "name": "Evan"
+ }
+ ]
+ }
+ },
+ {
+ "game_id": 2,
+ "away_team": {
+ "users": [
+ {
+ "id": 1,
+ "name": "Kiran"
+ }
+ ]
+ }
+ }
+ ],
+ "status": 200,
+ "statusText": "OK"
+ }
+ ```
+ description: |
+ You can use the result of a joined table to gather data in
+ another foreign table. With multiple references to the same foreign
+ table you must specify the column on which to conduct the join.
+ hideCodeBlock: true
- id: filtering-through-referenced-tables
name: Filtering through referenced tables
code: |