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.

Custom joins with Sequelize.literal

See original GitHub issue

Issue Description

When you need to add some custom table or a join for a report, there is not always an association defined in the models. For this other ORMs like Rails’ active record allow you to define custom joins without setting up an association.

Example

models.user.findAll({ include: [ {model: models.user_report, as: 'reports', on: Sequelize.literal('user.id = reports.user_id')} ] ...

For this to work I have to have a user to user_report association. This is not always possible and inconvenient to always define.

Describe the solution you’d like

The easiest this would be to allow a completely custom Sequelize.literal() inside the include:

include[ {Sequelize.literal( ' INNER JOIN user_report reports ON user.id = reports.user_id' )} ]

Why should this be in Sequelize

This is how it is done with other ORMs. As more users switch from Rails, Django and Laravel to node, they will be looking for something like this. There are several Stack Overflow questions asking for this. Ex: https://stackoverflow.com/questions/47215961/sequelize-js-join-tables-without-associations

Many issues brought up in the sequelize github could also be easier resolved with this as opposed to looking for all the operators etc within the include clause. Ex: https://github.com/sequelize/sequelize/issues/6718

Describe alternatives/workarounds you’ve considered

The only way I see to not have an association and still joining is a raw query, which erases the convenience of the ORM

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:25
  • Comments:27 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
gavinbelsoncommented, Nov 30, 2021

Does anyone know a project maintainer? This is a key issue for sequelize popularity

2reactions
gavinbelsoncommented, Jan 17, 2020

@papb That’s close, but a subquery creates a whole new query inside the original query.

We just need to be able to define the literal JOIN to add onto the original query.

For example, we just need to be able to add this literal SQL to the query created by sequelize: INNER JOIN user_report reports ON user.id = reports.user_id

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using literal in include (literal join) with sequelize
you can call the stored procedure and then make your join: const newTable = await sequelize.query( 'CALL MyStoredProcedures (:param1, ...
Read more >
Sub Queries - Sequelize
literal inserts arbitrary content without escaping to the query, it deserves very special attention since it may be a source of (major) security...
Read more >
sequelize query use column name instead model name
The model tells Sequelize several things about the entity it represents, such as the name of the table in the database and which...
Read more >
Sequelize Aggregate Functions (MIN, MAX, SUM, COUNT, etc ...
Most of you're using ORM for connecting your application to database. For those using SQL databases in Node.js platform, Sequelize is one of ......
Read more >
How to create JOIN queries with Sequelize - Nathan Sebhastian
Create JOIN queries using Sequelize.query() ... The sequelize.query() method is a function that allows you to write and run a raw SQL query....
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