dshukertjr
dshukertjr

Reputation: 18670

How to query using join in Supabase?

In Supabase documentation, it explains this as how you would "join" tables to get data

const { data, error } = await Supabase
  .from('countries')
  .select(`
    name,
    cities (
      name
    )
  `)

But how do I know this works every time when I have not specified which columns would be joined? Is there a way to specify which column to perform join on?

Upvotes: 34

Views: 54682

Answers (1)

dshukertjr
dshukertjr

Reputation: 18670

So this code works when there is only one relation(foreign key) between the two table countries and cities

const { data, error } = await Supabase
  .from('countries')
  .select(`
    name,
    cities (
      name
    )
  `)

Or when you want to join multiple tables, you can do this:

const { data, error } = await supabase
  .from('products')
  .select(`
    id,
    supplier:supplier_id ( name ),
    purchaser:purchaser_id ( name )
  `)

Upvotes: 52

Related Questions