Search code examples
supabasesupabase-database

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?


Solution

  • 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 )
      `)