Search code examples
javascriptsupabase

How to pull data from multi tables at once from supabase tables?


I have some tables on supabase db.
There is a post table and it contains user_id from profiles table.
user_id field related with the id of profiles table.
I am using @supabase node module for pulling data from supabase.
I can pull data with single query, but I don't know the way of pulling data from multiple tables with a query.
I followed this answer, but it doesn't help me. The response's result doesn't contain the profile information.
Here is the code I tried to use.

await supabase
  .from('posts')
  .select(`user_id,
    profiles(
      first_name
    )
  `);

How can I solve this problem?


Solution

  • Assuming you have a foreign key relationship between your user_id column and the profiles table, the following will return you the posts data as well as the profiles data of user_id on each posts row.

    const { data, error } = await supabase
      .from('posts')
      .select(`*,
        profiles(
          first_name
        )
      `);
    

    If this does not work, you probably do not have a foreign key relationship between the user_id column and profiles table. You can read the official guide on how to add foreign key relationship. https://supabase.com/docs/guides/database/tables#joining-tables-with-foreign-keys