mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem Readers who know the SQL join they want have to work out the matching embedding syntax by hand. The joins and nesting guide never mentions the SQL to REST API Translator, which does that conversion, so readers don't find it. Related to [FDBKIN-7994](https://linear.app/supabase/issue/FDBKIN-7994/improve-documentation-and-tooling-for-postgrest-join-syntax) Closes [DOCS-1729](https://linear.app/supabase/issue/DOCS-1729) ## Solution In the "Join types and join modifiers" section of `/guides/database/joins-and-nesting`, a new paragraph in the section's intro points readers who think in SQL to the translator. The paragraph says that the translator turns a SQL `select` query into a Data API request and Supabase JavaScript client code, with SQL joins as embedded relations. It also says that not every query translates, and names the translator's join limits: it handles only left and inner joins that compare columns with `=`, and the request it returns works only when a foreign key links the two tables. The paragraph follows the intro's explanation of left joins and `!inner`, so the translator's left-and-inner limit builds on it, and readers see the shortcut before the syntax table and the example tabs. Existing text is unchanged. ## How to verify 1. On the docs preview, open `/docs/guides/database/joins-and-nesting` and go to "Join types and join modifiers". 2. After the paragraph that ends "use `!inner` on the embedded relation", a paragraph names the SQL to REST API Translator and says that it converts SQL joins into embedded relations and client code. 3. The link opens `/docs/guides/api/sql-to-rest`. 4. The paragraph says that some queries won't translate. ## Checks Ran `pnpm turbo run typecheck --filter='...[origin/master]'`, `pnpm turbo run lint --filter='[origin/master]'`, and Prettier on the changed file. All pass. Skipped the docs tests, because they need Docker. Checked the facts against `apps/docs/content/guides/api/sql-to-rest.mdx` (the subset note), the translator component in `packages/ui-patterns/src/SqlToRest/` (cURL, HTTP, and JavaScript outputs), and the join handling in `@supabase/sql-to-rest` 0.1.6: it throws on join types other than inner and left and on join conditions other than `=`, and it does not check for a foreign key. ## Second reader First read: ready, no blockers, three nits. All three applied: the paragraph moved after the syntax table, two sentences merged, and the join limits named. Second read: one blocker. The join-limit sentence had the Data API as its subject, but the foreign key limit belongs to the translator, and the Data API also supports computed relationships. Fixed by making the translator the subject. Third read, after the first sentence was split in two: one blocker. The translator doesn't check foreign keys, so a join without one still translates, and the request then fails. Fixed: the sentence now says the translator handles only left and inner joins that compare columns with `=`, and that the request works only when a foreign key links the two tables. Nits left as they are: the translator outputs joins as flattened spread embeddings (`...relation(...)`), which this page doesn't explain; "client code" could read "supabase-js code". Fourth read, after the join-limits sentence was split in two: ready, no blockers. Nit left as it is: "For example" introduces translator limits after a sentence about Data API limits. Fifth read, after the paragraph moved into the section intro: ready, no blockers, and the placement matches the section's topic. Nits left as they are: when two foreign keys link the same tables, the translator's request is ambiguous and fails, because it never adds a foreign key hint (ambiguous foreign keys are out of scope here); and the last two sentences could form their own paragraph. Left as follow-ups outside the scope: the page intro says "data APIs" in lowercase, `guides/api/sql-to-api.mdx` doesn't link to the translator, and "We can get…" on the page uses _we_ for the reader. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Co-authored-by: Alexis Aguilar <98043211+alexisintech@users.noreply.github.com>
1063 lines
20 KiB
Plaintext
1063 lines
20 KiB
Plaintext
---
|
|
id: 'joins-and-nested-tables'
|
|
title: 'Querying Joins and Nested tables'
|
|
description: 'The Data APIs automatically detect relationships between Postgres tables.'
|
|
---
|
|
|
|
The data APIs automatically detect relationships between Postgres tables. Since Postgres is a relational database, this is a very common scenario.
|
|
|
|
## One-to-many joins
|
|
|
|
Use an example database that stores `orchestral_sections` and `instruments`:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="table"
|
|
queryGroup="output-format"
|
|
>
|
|
<TabPanel id="table" label="Tables">
|
|
|
|
**Orchestral sections**
|
|
|
|
| `id` | `name` |
|
|
| ---- | --------- |
|
|
| 1 | strings |
|
|
| 2 | woodwinds |
|
|
|
|
**Instruments**
|
|
|
|
| `id` | `name` | `section_id` |
|
|
| ---- | ------ | ------------ |
|
|
| 1 | violin | 1 |
|
|
| 2 | viola | 1 |
|
|
| 3 | flute | 2 |
|
|
| 4 | oboe | 2 |
|
|
|
|
</TabPanel>
|
|
<TabPanel id="SQL" label="SQL">
|
|
|
|
```sql
|
|
create table orchestral_sections (
|
|
"id" serial primary key,
|
|
"name" text
|
|
);
|
|
|
|
insert into orchestral_sections
|
|
(id, name)
|
|
values
|
|
(1, 'strings'),
|
|
(2, 'woodwinds');
|
|
|
|
create table instruments (
|
|
"id" serial primary key,
|
|
"name" text,
|
|
"section_id" int references "orchestral_sections"
|
|
);
|
|
|
|
insert into instruments
|
|
(name, section_id)
|
|
values
|
|
('violin', 1),
|
|
('viola', 1),
|
|
('flute', 2),
|
|
('oboe', 2);
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
The APIs will automatically detect relationships based on the foreign keys:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase.from('orchestral_sections').select(`
|
|
id,
|
|
name,
|
|
instruments ( id, name )
|
|
`)
|
|
```
|
|
|
|
### TypeScript types for joins
|
|
|
|
`supabase-js` always returns a `data` object (for success), and an `error` object (for unsuccessful requests).
|
|
|
|
These helper types provide the result types from any query, including nested types for database joins.
|
|
|
|
Given the following schema with a relation between orchestral sections and instruments:
|
|
|
|
```sql
|
|
create table orchestral_sections (
|
|
"id" serial primary key,
|
|
"name" text
|
|
);
|
|
|
|
create table instruments (
|
|
"id" serial primary key,
|
|
"name" text,
|
|
"section_id" int references "orchestral_sections"
|
|
);
|
|
```
|
|
|
|
We can get the nested `SectionsWithInstruments` type like this:
|
|
|
|
```ts
|
|
import { QueryData, QueryError, QueryResult } from '@supabase/supabase-js'
|
|
|
|
const sectionsWithInstrumentsQuery = supabase.from('orchestral_sections').select(`
|
|
id,
|
|
name,
|
|
instruments (
|
|
id,
|
|
name
|
|
)
|
|
`)
|
|
type SectionsWithInstruments = QueryData<typeof sectionsWithInstrumentsQuery>
|
|
|
|
const { data, error } = await sectionsWithInstrumentsQuery
|
|
if (error) throw error
|
|
const sectionsWithInstruments: SectionsWithInstruments = data
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase.from('orchestral_sections').select('id, name, instruments(id, name)');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
struct OrchestralSection: Codable {
|
|
let id: Int
|
|
let name: String
|
|
let instruments: [Instrument]
|
|
|
|
struct Instrument: Codable {
|
|
let id: Int
|
|
let name: String
|
|
}
|
|
}
|
|
|
|
let orchestralSections: [OrchestralSection] = try await supabase
|
|
.from("orchestral_sections")
|
|
.select("id, name, instruments(id, name)")
|
|
.execute()
|
|
.value
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val data = supabase.from("orchestral_sections").select(Columns.raw("id, name, instruments(id, name)"))
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = supabase.from_('orchestral_sections').select('id, name, instruments(id, name)').execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var result = await supabase
|
|
.From<OrchestralSection>()
|
|
.Select("id, name, instruments(id, name)")
|
|
.Get();
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="graphql" label="GraphQL">
|
|
|
|
```javascript
|
|
const Query = `
|
|
query {
|
|
orchestral_sectionsCollection {
|
|
edges {
|
|
node {
|
|
id
|
|
name
|
|
instruments {
|
|
id,
|
|
name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="url" label="URL">
|
|
|
|
```bash
|
|
GET https://[REF].supabase.co/rest/v1/orchestral_sections?select=id,name,instruments(id,name)
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Join types and join modifiers
|
|
|
|
By default, embedded relations use **left join** semantics from the parent table:
|
|
|
|
- Parent rows are returned even if no related rows match.
|
|
- The embedded relation is `[]` for one-to-many joins and `null` for many-to-one joins when nothing matches.
|
|
|
|
To filter out parent rows that do not match the related table, use `!inner` on the embedded relation.
|
|
|
|
If you know the join you want in SQL, use the [SQL to REST API Translator](/docs/guides/api/sql-to-rest). It converts a SQL `select` query into the matching Data API request and Supabase JavaScript client code, with joins as embedded relations. The Data API supports only a subset of SQL, so some queries won't translate. For example, the translator handles only left and inner joins that compare columns with `=`. The request it returns works only when a foreign key links the two tables.
|
|
|
|
### What `:` and `!` mean in join syntax
|
|
|
|
| Syntax | Meaning | Example |
|
|
| ------------------------------- | --------------------------------------------------------------------------------------- | --------------------------------------- |
|
|
| `alias:relation(columns)` | Rename the embedded relation in the response. | `start_scan:scans(id, badge_scan_time)` |
|
|
| `relation!inner(columns)` | Use `inner join` behavior for that embedded relation. | `instruments!inner(id, name)` |
|
|
| `relation!foreign_key(columns)` | Choose which foreign key relationship to use when multiple foreign keys match the join. | `scans!scan_id_start(id)` |
|
|
|
|
### Example data for join types
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="table"
|
|
queryGroup="join-types-example"
|
|
>
|
|
<TabPanel id="table" label="Tables">
|
|
|
|
#### Orchestral sections
|
|
|
|
| `id` | `name` |
|
|
| ---- | ---------- |
|
|
| 1 | strings |
|
|
| 2 | woodwinds |
|
|
| 3 | percussion |
|
|
|
|
#### Instruments
|
|
|
|
| `id` | `name` | `section_id` |
|
|
| ---- | ------ | ------------ |
|
|
| 1 | violin | 1 |
|
|
| 2 | viola | 1 |
|
|
| 3 | flute | 2 |
|
|
| 4 | oboe | 2 |
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
create table orchestral_sections (
|
|
"id" serial primary key,
|
|
"name" text
|
|
);
|
|
|
|
insert into orchestral_sections
|
|
(id, name)
|
|
values
|
|
(1, 'strings'),
|
|
(2, 'woodwinds'),
|
|
(3, 'percussion');
|
|
|
|
create table instruments (
|
|
"id" serial primary key,
|
|
"name" text,
|
|
"section_id" int references orchestral_sections
|
|
);
|
|
|
|
insert into instruments
|
|
(id, name, section_id)
|
|
values
|
|
(1, 'violin', 1),
|
|
(2, 'viola', 1),
|
|
(3, 'flute', 2),
|
|
(4, 'oboe', 2);
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
### Left join (default)
|
|
|
|
This query filters on a joined field (`instruments.name`) but still returns all parent rows:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="join-types-left"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('orchestral_sections')
|
|
.select(
|
|
`
|
|
id,
|
|
name,
|
|
instruments ( id, name )
|
|
`
|
|
)
|
|
.eq('instruments.name', 'flute')
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase
|
|
.from('orchestral_sections')
|
|
.select('''
|
|
id,
|
|
name,
|
|
instruments ( id, name )
|
|
''')
|
|
.eq('instruments.name', 'flute');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase
|
|
.from("orchestral_sections")
|
|
.select(
|
|
"""
|
|
id,
|
|
name,
|
|
instruments ( id, name )
|
|
"""
|
|
)
|
|
.eq("instruments.name", value: "flute")
|
|
.execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val columns = Columns.raw("""
|
|
id,
|
|
name,
|
|
instruments ( id, name )
|
|
""".trimIndent())
|
|
|
|
val data = supabase.from("orchestral_sections").select(
|
|
columns = columns
|
|
) {
|
|
filter {
|
|
eq("instruments.name", "flute")
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = (
|
|
supabase.from_('orchestral_sections')
|
|
.select('id, name, instruments(id, name)')
|
|
.eq('instruments.name', 'flute')
|
|
.execute()
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="url" label="URL">
|
|
|
|
```bash
|
|
GET https://[REF].supabase.co/rest/v1/orchestral_sections?select=id,name,instruments(id,name)&instruments.name=eq.flute
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
#### Result
|
|
|
|
```json
|
|
[
|
|
{
|
|
"id": 1,
|
|
"name": "strings",
|
|
"instruments": []
|
|
},
|
|
{
|
|
"id": 2,
|
|
"name": "woodwinds",
|
|
"instruments": [{ "id": 3, "name": "flute" }]
|
|
},
|
|
{
|
|
"id": 3,
|
|
"name": "percussion",
|
|
"instruments": []
|
|
}
|
|
]
|
|
```
|
|
|
|
### Inner join (`!inner`)
|
|
|
|
Adding `!inner` filters out parent rows that don't match the joined filter:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="join-types-inner"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('orchestral_sections')
|
|
.select(
|
|
`
|
|
id,
|
|
name,
|
|
instruments!inner ( id, name )
|
|
`
|
|
)
|
|
.eq('instruments.name', 'flute')
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase
|
|
.from('orchestral_sections')
|
|
.select('''
|
|
id,
|
|
name,
|
|
instruments!inner ( id, name )
|
|
''')
|
|
.eq('instruments.name', 'flute');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase
|
|
.from("orchestral_sections")
|
|
.select(
|
|
"""
|
|
id,
|
|
name,
|
|
instruments!inner ( id, name )
|
|
"""
|
|
)
|
|
.eq("instruments.name", value: "flute")
|
|
.execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val columns = Columns.raw("""
|
|
id,
|
|
name,
|
|
instruments!inner ( id, name )
|
|
""".trimIndent())
|
|
|
|
val data = supabase.from("orchestral_sections").select(
|
|
columns = columns
|
|
) {
|
|
filter {
|
|
eq("instruments.name", "flute")
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = (
|
|
supabase.from_('orchestral_sections')
|
|
.select('id, name, instruments!inner(id, name)')
|
|
.eq('instruments.name', 'flute')
|
|
.execute()
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="url" label="URL">
|
|
|
|
```bash
|
|
GET https://[REF].supabase.co/rest/v1/orchestral_sections?select=id,name,instruments!inner(id,name)&instruments.name=eq.flute
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
#### Result [#inner-join-inner-result]
|
|
|
|
```json
|
|
[
|
|
{
|
|
"id": 2,
|
|
"name": "woodwinds",
|
|
"instruments": [{ "id": 3, "name": "flute" }]
|
|
}
|
|
]
|
|
```
|
|
|
|
### Filtering using joined fields
|
|
|
|
Use `joined_table.column` in filters (for example `eq`, `neq`, and `in`):
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="join-types-filtering"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('instruments')
|
|
.select(
|
|
`
|
|
id,
|
|
name,
|
|
orchestral_sections!inner ( id, name )
|
|
`
|
|
)
|
|
.eq('orchestral_sections.name', 'woodwinds')
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase
|
|
.from('instruments')
|
|
.select('''
|
|
id,
|
|
name,
|
|
orchestral_sections!inner ( id, name )
|
|
''')
|
|
.eq('orchestral_sections.name', 'woodwinds');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase
|
|
.from("instruments")
|
|
.select(
|
|
"""
|
|
id,
|
|
name,
|
|
orchestral_sections!inner ( id, name )
|
|
"""
|
|
)
|
|
.eq("orchestral_sections.name", value: "woodwinds")
|
|
.execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val columns = Columns.raw("""
|
|
id,
|
|
name,
|
|
orchestral_sections!inner ( id, name )
|
|
""".trimIndent())
|
|
|
|
val data = supabase.from("instruments").select(
|
|
columns = columns
|
|
) {
|
|
filter {
|
|
eq("orchestral_sections.name", "woodwinds")
|
|
}
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = (
|
|
supabase.from_('instruments')
|
|
.select('id, name, orchestral_sections!inner(id, name)')
|
|
.eq('orchestral_sections.name', 'woodwinds')
|
|
.execute()
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="url" label="URL">
|
|
|
|
```bash
|
|
GET https://[REF].supabase.co/rest/v1/instruments?select=id,name,orchestral_sections!inner(id,name)&orchestral_sections.name=eq.woodwinds
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
#### Result [#filtering-using-joined-fields-result]
|
|
|
|
```json
|
|
[
|
|
{
|
|
"id": 3,
|
|
"name": "flute",
|
|
"orchestral_sections": {
|
|
"id": 2,
|
|
"name": "woodwinds"
|
|
}
|
|
},
|
|
{
|
|
"id": 4,
|
|
"name": "oboe",
|
|
"orchestral_sections": {
|
|
"id": 2,
|
|
"name": "woodwinds"
|
|
}
|
|
}
|
|
]
|
|
```
|
|
|
|
## Many-to-many joins
|
|
|
|
The data APIs will detect many-to-many joins. For example, if you have a database which stored teams of users (where each user could belong to many teams):
|
|
|
|
```sql
|
|
create table users (
|
|
"id" serial primary key,
|
|
"name" text
|
|
);
|
|
|
|
create table teams (
|
|
"id" serial primary key,
|
|
"team_name" text
|
|
);
|
|
|
|
create table members (
|
|
"user_id" int references users,
|
|
"team_id" int references teams,
|
|
primary key (user_id, team_id)
|
|
);
|
|
```
|
|
|
|
In these cases you don't need to explicitly define the joining table (members). If we wanted to fetch all the teams and the members in each team:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase.from('teams').select(`
|
|
id,
|
|
team_name,
|
|
users ( id, name )
|
|
`)
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase.from('teams').select('id, team_name, users(id, name)');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
struct Team: Codable {
|
|
let id: Int
|
|
let name: String
|
|
let users: [User]
|
|
|
|
struct User: Codable {
|
|
let id: Int
|
|
let name: String
|
|
}
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id, users
|
|
case name = "team_name"
|
|
}
|
|
}
|
|
let teams [Team] = try await supabase
|
|
.from("teams")
|
|
.select(
|
|
"""
|
|
id,
|
|
team_name,
|
|
users ( id, name )
|
|
"""
|
|
)
|
|
.execute()
|
|
.value
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:kotlin">
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val data = supabase.from("teams").select(Columns.raw("id, team_name, users(id, name)"));
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = supabase.from_('teams').select('id, team_name, users(id, name)').execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var result = await supabase
|
|
.From<Team>()
|
|
.Select("id, team_name, users(id, name)")
|
|
.Get();
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="graphql" label="GraphQL">
|
|
|
|
```javascript
|
|
const Query = `
|
|
query {
|
|
teamsCollection {
|
|
edges {
|
|
node {
|
|
id
|
|
team_name
|
|
users {
|
|
id,
|
|
name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="url" label="URL">
|
|
|
|
```bash
|
|
GET https://[REF].supabase.co/rest/v1/teams?select=id,team_name,users(id,name)
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
## Specifying the `ON` clause for joins with multiple foreign keys
|
|
|
|
For example, if you have a project that tracks when employees check in and out of work shifts:
|
|
|
|
```sql
|
|
-- Employees
|
|
create table users (
|
|
"id" serial primary key,
|
|
"name" text
|
|
);
|
|
|
|
-- Badge scans
|
|
create table scans (
|
|
"id" serial primary key,
|
|
"user_id" int references users,
|
|
"badge_scan_time" timestamp
|
|
);
|
|
|
|
-- Work shifts
|
|
create table shifts (
|
|
"id" serial primary key,
|
|
"user_id" int references users,
|
|
"scan_id_start" int references scans, -- clocking in
|
|
"scan_id_end" int references scans, -- clocking out
|
|
"attendance_status" text
|
|
);
|
|
```
|
|
|
|
In this case, you need to explicitly define the join because the joining column on `shifts` is ambiguous as they are both referencing the `scans` table.
|
|
|
|
To fetch all the `shifts` with `scan_id_start` and `scan_id_end` related to a specific `scan`, use the following syntax:
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="js"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="js" label="JavaScript">
|
|
|
|
```js
|
|
const { data, error } = await supabase.from('shifts').select(
|
|
`
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
`
|
|
)
|
|
```
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
final data = await supabase.from('shifts').select('''
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
''');
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
struct Shift: Codable {
|
|
let id: Int
|
|
let userId: Int
|
|
let attendanceStatus: String?
|
|
|
|
let startScan: Scan
|
|
let endScan: Scan
|
|
|
|
struct Scan: Codable {
|
|
let id: Int
|
|
let userId: Int
|
|
let badgeScanTime: TimeInterval
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case userId = "user_id"
|
|
case badgeScanTime = "badge_scan_time"
|
|
}
|
|
}
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case id
|
|
case userId = "user_id"
|
|
case attendanceStatus = "attendance_status"
|
|
case startScan = "start_scan"
|
|
case endScan = "end_scan"
|
|
}
|
|
}
|
|
|
|
let shifts: [Shift] = try await supabase
|
|
.from("shifts")
|
|
.select(
|
|
"""
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
"""
|
|
)
|
|
.execute()
|
|
.value
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
|
|
<$Show if="sdk:kotlin">
|
|
|
|
<TabPanel id="kotlin" label="Kotlin">
|
|
|
|
```kotlin
|
|
val data = supabase.from("shifts").select(Columns.raw('''
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
'''));
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
data = supabase.from_('shifts').select("""
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
""").execute()
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var result = await supabase
|
|
.From<Shift>()
|
|
.Select(@"
|
|
*,
|
|
start_scan:scans!scan_id_start (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
),
|
|
end_scan:scans!scan_id_end (
|
|
id,
|
|
user_id,
|
|
badge_scan_time
|
|
)
|
|
")
|
|
.Get();
|
|
```
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<TabPanel id="graphql" label="GraphQL">
|
|
|
|
```javascript
|
|
const Query = `
|
|
query {
|
|
shiftsCollection {
|
|
edges {
|
|
node {
|
|
id
|
|
user_id
|
|
attendance_status
|
|
scan_id_start {
|
|
id
|
|
user_id
|
|
badge_scan_time
|
|
}
|
|
scan_id_end {
|
|
id
|
|
user_id
|
|
badge_scan_time
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|