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.

Sequelize should combine duplicated properties in the WHERE clauses of multiple scopes by ANDing them instead of replacing them

See original GitHub issue

Issue Description

Is your feature request related to a problem? Please describe.

Sequelize’s “combine where clause when using multiple scopes” feature is severely limited because it replaces duplicated properties instead of ANDing them.

For example combining

where: { id: 1 }

with

where: { company_id: 2 }

results in the expected

where: { id: 1, company_id: 2}

This is the equivalent of combining

WHERE id = 1

with

WHERE company_id = 2

to produce

WHERE id = 1 AND company_id = 2

However, if I wanna combine, e.g.

WHERE id = 1 OR status = 'P'

with

WHERE company_id = 2 OR company_status = 'P'

to produce

WHERE (id = 1 OR status = 'P') AND (company_id = 3 OR company_status = 'P')

I CANNOT do this with combined scopes. Because if I combine

where: { [Op.or]: { id: 1, status: 'P' } }

with

where: { [Op.or]: { id: company_id, company_status: 'P' } }

I get

where: { [Op.or]: { id: company_id, company_status: 'P' } }

Meaning the latter is just overwriting the former. Another example that does NOT work is combining

where: { id: { [Op.gt]: 5 } }

with

where: { id: { [Op.lt]: 50 } }

which would have been super useful.

Describe the solution you’d like

The trouble is that when Sequelize finds a duplicate key in the where clauses of multiple scopes, whether it is a column name, or an Op.* operator, it just replaces them!

This also means that Model.scope([a, b]) is not guaranteed to be Model.scope([a, b]).

My solution is to first realize that

where: { id: 1, company_id: 2}

is really a shorthand for

where: {
  [Op.and]: [ { id: 1 }, { company_id: 2} ]
}

So Sequelize should combine duplicated properties in multiple scopes by ANDing them together.

So combining

where: { id: 1 }

with

where: { company_id: 2 }

makes

where: { 
  [Op.and]: [
    { id: 1 }
    { company_id: 2 }
  ]
}

which is the same as

where: { id: 1, company_id: 2}

And now the other combinations work too. E.g.

where: { [Op.or]: { id: 1, status: 'P' } }

with

where: { [Op.or]: { id: company_id, company_status: 'P' } }

makes

where: {
  [Op.and]: [
    { [Op.or]: { id: 1, status: 'P' } }
    { [Op.or]: { id: company_id, company_status: 'P' } }
  ]
}

And combining

where: { id: { [Op.gt]: 5 } }

with

where: { id: { [Op.lt]: 50 } }

makes

where: {
  [Op.and]: [
    { id: { [Op.gt]: 5 } }
    { id: { [Op.lt]: 50 } }
  ]
}

Why should this be in Sequelize

Scopes is a core part of Sequelize. I am unsure if my proposal is possible in a plugin. In addition, based on numerous other tickets and stack overflow questions, my proposed behavior is what a lot (perhaps even most) people expect, and are surprised when it isn’t.

Describe alternatives/workarounds you’ve considered

= N.A. =

Additional context

= N.A. =

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:closed
  • Created 4 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
lobancommented, Oct 21, 2019

Okely Dokely. I’ll try to carve out some time to take a look at it this week. Thanks for your prompt responses!

1reaction
lobancommented, Jan 21, 2020

Unfortunately, no - I kinda dropped the ball on this. I was unable to make time. But I still long for this, so I haven’t completely dropped it from my todo list just yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scopes - Sequelize
Scopes are used to help you reuse code. You can define commonly used queries, specifying options such as where , include , limit...
Read more >
Using Sequelize with associations and scopes with includes ...
node. js - Using Sequelize with associations and scopes with includes in multiple files - Stack Overflow. Stack Overflow for Teams – Start...
Read more >
Changelog - 《Sequelize ORM v3 Document》 - 书栈网
This means you should change all promise listeners on sequelize.query to use .spread instead of .then , unless you are passing a query...
Read more >
Untitled
About land rover discovery 2, Conad via fabrateria vetus ceccano, Knife painting tree, Corrs white light review, Marillion sounds that can't be made...
Read more >
All Blog Posts by Ben Nadel
Read all articles that Ben Nadel has evern written on web development, 2006 through 2022. Topics range from ColdFusion and CFML to HTML...
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