Hey all, quick Q about joins and such. I have 3 ta...
# javascript
y
Hey all, quick Q about joins and such. I have 3 tables:
users
,
groups
and
groups_users
. The last one is a table containing `userId`/`groupId` links, to join between them. What would be the ideal queries to get "All users of group `i`" and "All groups of user `j`"?
I got a SQL query working that simply does the following:
Copy code
SELECT *
FROM summoners summ
INNER JOIN group_summoner gs ON summ."summonerId" = gs."summonerId"
(summoners are kind of users)
But with the JS API I do the following:
Copy code
const { data, error, status } = await this.supabaseClient
      .from<definitions['group_summoner']>('groups_summoners')
      .select(
        `
        *,
        summoners (
          name
   )`,
      )
      .eq('groupId', id)
and I get
Could not find a relationship between groups_summoners and summoners in the schema cache
Not the same query I know, but seems to be a similar logic?
Here is my ideal query I think:
Copy code
SELECT *
FROM summoners summ
INNER JOIN groups gr ON gr.id = 1
INNER JOIN group_summoner gs ON summ."summonerId" = gs."summonerId";
I managed to do it with something "simpler":
Copy code
const { data, error, status } = await this.supabaseClient
      .from<definitions['groups']>('groups')
      .select(
        `
        *,
        summoners (
          name
        )
        `,
      )
      .eq('id', id);
I figure supabase/postgres knows how to follow the foreign key alone?
I feel there's some magic happening and would like to understand more :/ @User
s
Under the hood the Supabase library is using postgrest for its querying mechanisms, you can read more about how it handles relationships on their website https://postgrest.org/en/v9.0/api.html?highlight=relationships#embedding-through-join-tables
y
That's pretty cool! Thanks for the info 🎉