sequelize / sequelize/sequelize

Incorrect ordering for a findAll operation on a scoped model with predefined order

Open
#11,415 15 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first issue status: wip type: bug
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

What are you doing?

I have a Model TheModel with a scope scoped containing order , say order1.
On invoking a findAll with an order, say order2, the resulting order is order1, order2

// MINIMAL, SELF-CONTAINED code here (SSCCE/MCVE/reprex)
TheModel.addScope('scoped', {
  order: [
    [associatedModel1, 'foo', 'ASC'],
    [associatedModel2, 'bar', 'ASC'],
  ]
});

scopedModel.findAll({
  order: [
    ['field1', 'ASC'],
    [associatedModel1, 'foo', 'ASC'],
  ]
});
What do you expect to happen?

Resulting order: ORDER BY field1 ASC, associatedModel1.foo ASC
(This was the actual order using sequelize version 3.30.4)

What is actually happening?

Resulting order: ORDER BY associatedModel1.foo ASC, associatedModel2.bar ASC, field1 ASC, associatedModel1.foo ASC

Environment
  • Sequelize version: 5.15.2
  • Node.js version: 6.13.4
  • Operating System: XXX
  • If TypeScript related: TypeScript version: XXX

Issue Template Checklist

How does this problem relate to dialects?
  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
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.

Note: I was able to come to this line which is merging the two orders. However, I think the community needs to collectively decide what should be the expected behaviour. I think that the order provided by findAll should always override the scope order.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal scoped-model example and inspect lib/model.js around line 798, where the two orders are merged. Confirm the intended precedence with the existing discussion, then add coverage for the agreed behavior. Done means findAll produces the expected ORDER BY sequence without unintended duplicated scope entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, sql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.