question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request - RIGHT JOIN

See original GitHub issue

Given that we currently cannot limit the associated records (#1897), I’ve been exploring other options; one of the ideas is to select the associated records and right-join onto a particular main record in order to retrieve both the main and associated records.

Consider the case where there is a post, and that post may or may not have comments, and no more than 5 latest comments are to be retrieved:

// Ideally, we'd just do this in order to get 1 post with 0-5 latest comments:
Post.findOne({
  where: { id: 123 },
  include: [{
    model: Comment,
    required: false,
    order: [ ['timestamp'], 'DESC'] ]
    limit: 5
  }]
});

One of the workarounds I’m exploring is to select the comments of a post and include the post:

// This only retrieves latest 1-5 comments for the post along with the post.
// If the post has no comments, result set would be empty, obviously.
Comment.findOne({
  where: { post_id: 123 },
  include: [{
    model: Post,
    required: false
  }],
  order: [ ['timestamp', 'DESC'] ],
  limit: 5
});

A similar RIGHT JOIN would be able to retrieve 0-5 latest comments along with the post regardless there are comments or not:

SELECT c.*, p.*
FROM Comments AS c
RIGHT JOIN Posts AS p ON c.post_id = p.id
WHERE p.id = 123

// Perhaps something like:
Comment.findOne({
  //where: { post_id: 123 }, // Not sure if this still makes sense.
  include: [{
    model: Post,
    where: { id: 123 },
    required: false
  }],
  order: [ ['timestamp', 'DESC'] ],
  limit: 5
});

@janmeier @mickhansen Is this something that could be implemented for the time being while #1897 is being worked on? Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
TakanashiOukencommented, Jun 25, 2019

This was 4 years ago. But we still don’t have RIGHT OUTER JOIN for Sequelize ?

4reactions
marc-ferrercommented, Jul 10, 2017

That’s why i did not reference any of you in my comment. I just could not find a better place to ask this, than an issue called “Feature Request - RIGHT JOIN”

I appreciate your help, I guess I could use raw queries as you suggest and populate the models manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Join Feature Requests
When the laptop is not connected to the monitor, I cannot "find" the Join window. There is not an option to move the...
Read more >
Feature Request Template: How to Manage Suggestions at ...
Streamline and organize user feedback with this free feature request template. Available in Google Docs and Sheets (no email required).
Read more >
Flow/merge node - left & right join - Feature Requests - n8n
The idea is: right now the merge node has two modes that can be compared to SQL join: merge by key - behaves...
Read more >
Is this the right forum for feature requests?
Solved: Hi, just wondering how new features should be requested, voted on etc.
Read more >
The Top 10 Feature Requests for SQL Server on Connect ...
Microsoft.com and upvotes her feature requests. ... leave a synonym behind, and let your app keep right on trucking without noticing.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found