postgresqljoinsupabasesupabase-database

Supabase join table query returns null if foreign key is added


So I'm learning Supabase, but I got stuck on a query. I reproduced this docs example: https://supabase.com/docs/reference/javascript/select, with the example "Query referenced tables through a join table". I added the example data source in the SQL Editor and created those tables.

I tested it with the example query and it gave the expected reponse:

const { data, error } = await supabase
  .from('users')
  .select(`
    name,
    teams (
      name
    )
  `)

However, when I added a new column (trainer_id, which is a foreign key to the users.id) in the teams table, that same query returns null.

Why does it return null if a foreign key is added? Should I change my query? The goal is to get the exact same response as in the example.


Solution

  • So I found the answer, it was because there were two relationships after I added a foreign key in the poules table.

    Answer:

    const { data, error } = await supabase.from('users').select(`
      name,
      teams!users_teams (
        name
      )
    `);