0

I have 2 tables as shown below where user table stores the users profile details and friends table which stores the friendship relation between 2 users where sent column links with user who has sent the request and recieved column links with user who gets the request notification to accept.

user {
   id   uuid
   name string
}

friends {
   id         id
   sent       uuid (relation: user.id)
   recieved   uuid (relation: user.id)
   accepted   boolean
}

I want to query all the friends table where user's id is present either in sent or recieved field.

I can achieve this using

const { data, error } = await supabase
        .from('friends')
        .select('*')
        .eq('accepted', true)
        .or(`sent.eq.${user?.id},recieved.eq.${user?.id}`);

But the twist is i also want the joint tables of users (sent and request) along with it. Something like

const { data, error } = await supabase
        .from('friends')
        .select('*, recieved(*), sent(*)')
        .eq('accepted', true)
        .or(`sent.id.eq.${user?.id},recieved.id.eq.${user?.id}`);

But the following gives error since both fields come from 2 different foreign tables

Deep Shetye
  • 143
  • 1
  • 7
  • 1
    For anything complex like this, it's easier to just use database functions: https://supabase.com/docs/guides/database/functions – dshukertjr Jul 19 '23 at 05:48
  • 1
    Thanks for the reply @dshukertjr! I exactly thought of this and created a function with a sql query to match my requirements – Deep Shetye Jul 19 '23 at 22:44

0 Answers0