Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Get pending friend requests from one table

Im trying to get all pending friend requests from a table with schema:

id user_id friend_id

A pending request would just be a single row such as:

(Meaning user 1 sent a request to user 2)

id user_id friend_id
1 1 2

and when accepted it becomes two rows, and I am able to join two of this table to find all accepted (this is working just fine).

An accepted request for reference:

id user_id friend_id
1 1 2
2 2 1

My accepted query looks like this (Im using bookshelf js and knex.js):

const friends = new Friends();
    return friends.query((qb) => {
        qb.select('friends.user_id', 'friends.friend_id');
        qb.join('friends as friendsTwo', 'friends.user_id', 'friendsTwo.friend_id');
        qb.where('friends.user_id', '=', id);
    }).fetchAll();

How can I modify this to only get the one way relationships? My first thought was leftJoin and I couldnt seem to get it to work, so if anyone knows an answer or has seen a good answer please lead me to it, thanks :).

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you can completely go away from idea of join in this project. I had done something similar in my previous projects and I think it will be best to add a third field isMutual meaning if they are mutual or not. The field is self-explanatory and think you got the idea. After that your table must look like

No accepted friendship

id user_id friend_id isMutual
1 1 2 false

Accepted friendship

id user_id friend_id isMutual
1 2 1 true
2 1 2 true

I believe doing this will actually benefit your system as your query will be faster and try making an compound index for (user_id, friend_id). This solution is more towards shifting the load.

Conclusion

Using this, you can achieve faster queries, but all the hard work done in during READ OPERATION in previous schema will shift to WRITE OPERATION. But I think this will reduce writing speed by more. And yeah, make sure you use transaction doing updates for isMutual field.

about 4 years ago · Juan Pablo Isaza Report

0

qb.leftJoin('friends as friendsTwo', 'friends.user_id', 'friendsTwo.friend_id');
qb.whereNull('friendsTwo.user_id');

This will LEFT JOIN, which keeps all rows (pending or accepted), but then filter to keep only those with no matching record in friendsTwo; thus returning only the pending links.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!