falcon
falcon

Reputation: 111

Supabase SQL question regarding foreign key

I have two tables.

  Users
   - id (primary key)
   - firstName
   - lastName
   - userName
   - email
   
   Followers

   - user (foreign key to Users)
   - following (foreign key to Users)

I want to get all the non-followed users by the current users.

Here is my current query

let { data: followers, error } = await supabase
        .from("Users")
        .select("*, followers!inner(*)")
        .eq("followers.user", user.id);

Upvotes: 0

Views: 518

Answers (1)

Raky
Raky

Reputation: 902

To get all the non-followed users by the current user, you can use a combination of not and in operators along with a subquery to exclude the users who are being followed by the current user.

Please try this

// Assuming user.id is the ID of the current user
let { data: nonFollowedUsers, error } = await supabase
  .from("Users")
  .select("*")
  .not("id", "in", supabase
    .from("Followers")
    .select("following")
    .eq("user", user.id)
  );

console.log(nonFollowedUsers);

Upvotes: 0

Related Questions