Repository navigation
Open
Labels
topic: group & orderFor issues and PRs. Things that involve grouping and ordering.topic: limit & offsetFor issues and PRs. Things that involve queries with limit and/or offset.type: bugDEPRECATED: replace with the "bug" issue type
Description
What are you doing?
npm install sequelize
npm install sqlite3'use strict'
const Sequelize = require('sequelize')
const sequelize = new Sequelize({dialect: 'sqlite', storage: 'test.sqlite'})
const Review = sequelize.define('reviews', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
project_id: {
type: Sequelize.INTEGER
},
text: {
type: Sequelize.STRING
}
})
const Project = sequelize.define('projects', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
author_id: {
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
}
})
const Author = sequelize.define('authors', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
organization_id: {
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
}
})
const Organization = sequelize.define('organizations', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
name: {
type: Sequelize.STRING,
}
})
Project.hasMany(Review, {foreignKey: 'project_id', sourceKey: 'id'})
Review.belongsTo(Project, {foreignKey: 'id'})
Author.hasMany(Project, {foreignKey: 'author_id', sourceKey: 'id'})
Project.belongsTo(Author, {foreignKey: 'id'})
Organization.hasMany(Author, {foreignKey: 'organization_id', sourceKey: 'id'})
Author.belongsTo(Organization, {foreignKey: 'id'})
sequelize
.sync({force: true})
.then(() => {
return Project.findAll({
offset: 0,
limit: 100,
include: [
{model: Author, include: [{model: Organization}]}
],
order: [[Author, Organization, 'name', 'ASC']]
})
}).then(() => {
return Project.findAll({
offset: 0,
limit: 100,
include: [
{model: Author, include: [{model: Organization}]},
{model: Review}
],
order: [[Author, Organization, 'name', 'ASC']]
})
}).then(results => {
console.log(results)
})What do you expect to happen?
The two queries should handle LIMIT and ORDER the same way (in the outermost scope), regardless of how many includes they have.
SELECT `projects`.`id`, `projects`.`author_id`, `projects`.`name`, `projects`.`createdAt`, `projects`.`updatedAt`, `author`.`id` AS `author.id`, `author`.`organization_id` AS `author.organization_id`, `author`.`name` AS `author.name`, `author`.`createdAt` AS `author.createdAt`, `author`.`updatedAt` AS `author.updatedAt`, `author->organization`.`id` AS `author.organization.id`, `author->organization`.`name` AS `author.organization.name`, `author->organization`.`createdAt` AS `author.organization.createdAt`, `author->organization`.`updatedAt` AS `author.organization.updatedAt` FROM `projects` AS `projects` LEFT OUTER JOIN `authors` AS `author` ON `projects`.`id` = `author`.`id` LEFT OUTER JOIN `organizations` AS `author->organization` ON `author`.`id` = `author->organization`.`id` ORDER BY `author->organization`.`name` ASC LIMIT 0, 100;
SELECT `projects`.`id`, `projects`.`author_id`, `projects`.`name`, `projects`.`createdAt`, `projects`.`updatedAt`, `author`.`id` AS `author.id`, `author`.`organization_id` AS `author.organization_id`, `author`.`name` AS `author.name`, `author`.`createdAt` AS `author.createdAt`, `author`.`updatedAt` AS `author.updatedAt`, `author->organization`.`id` AS `author.organization.id`, `author->organization`.`name` AS `author.organization.name`, `author->organization`.`createdAt` AS `author.organization.createdAt`, `author->organization`.`updatedAt` AS `author.organization.updatedAt` FROM `projects` AS `projects` LEFT OUTER JOIN `authors` AS `author` ON `projects`.`id` = `author`.`id` LEFT OUTER JOIN `organizations` AS `author->organization` ON `author`.`id` = `author->organization`.`id` LEFT OUTER JOIN `reviews` AS `reviews` ON `projects`.`id` = `reviews`.`project_id` ORDER BY `author->organization`.`name` ASC LIMIT 0, 100;What is actually happening?
In the first case (without including Review), a correct SQL is generated. In the second case (including Review), LIMIT is applied in the subquery, breaking the ORDER.
SELECT `projects`.`id`, `projects`.`author_id`, `projects`.`name`, `projects`.`createdAt`, `projects`.`updatedAt`, `author`.`id` AS `author.id`, `author`.`organization_id` AS `author.organization_id`, `author`.`name` AS `author.name`, `author`.`createdAt` AS `author.createdAt`, `author`.`updatedAt` AS `author.updatedAt`, `author->organization`.`id` AS `author.organization.id`, `author->organization`.`name` AS `author.organization.name`, `author->organization`.`createdAt` AS `author.organization.createdAt`, `author->organization`.`updatedAt` AS `author.organization.updatedAt` FROM `projects` AS `projects` LEFT OUTER JOIN `authors` AS `author` ON `projects`.`id` = `author`.`id` LEFT OUTER JOIN `organizations` AS `author->organization` ON `author`.`id` = `author->organization`.`id` ORDER BY `author->organization`.`name` ASC LIMIT 0, 100;
SELECT `projects`.*, `author`.`id` AS `author.id`, `author`.`organization_id` AS `author.organization_id`, `author`.`name` AS `author.name`, `author`.`createdAt` AS `author.createdAt`, `author`.`updatedAt` AS `author.updatedAt`, `author->organization`.`id` AS `author.organization.id`, `author->organization`.`name` AS `author.organization.name`, `author->organization`.`createdAt` AS `author.organization.createdAt`, `author->organization`.`updatedAt` AS `author.organization.updatedAt`, `reviews`.`id` AS `reviews.id`, `reviews`.`project_id` AS `reviews.project_id`, `reviews`.`text` AS `reviews.text`, `reviews`.`createdAt` AS `reviews.createdAt`, `reviews`.`updatedAt` AS `reviews.updatedAt` FROM (SELECT `projects`.`id`, `projects`.`author_id`, `projects`.`name`, `projects`.`createdAt`, `projects`.`updatedAt` FROM `projects` AS `projects` LIMIT 0, 100) AS `projects` LEFT OUTER JOIN `authors` AS `author` ON `projects`.`id` = `author`.`id` LEFT OUTER JOIN `organizations` AS `author->organization` ON `author`.`id` = `author->organization`.`id` LEFT OUTER JOIN `reviews` AS `reviews` ON `projects`.`id` = `reviews`.`project_id` ORDER BY `author->organization`.`name` ASC;Dialect: postgres / sqlite
Dialect version: Postgres 9.6.1 / SQLite3
Sequelize version: 4.13.8
Tested with master branch: Yes
Reacted by Mohamed Habashy, Ryan Lindgren, blaivs, Konstantin V. Salikhov, shivani19, Mia Pelino and bulolo
Metadata
Metadata
Assignees
Labels
topic: group & orderFor issues and PRs. Things that involve grouping and ordering.topic: limit & offsetFor issues and PRs. Things that involve queries with limit and/or offset.type: bugDEPRECATED: replace with the "bug" issue type
Type
Projects
- StatusShow more project fieldsNo status
Activity
krzysztofczernek commentedon Oct 10, 2017
On a side note:
separate: trueis not always a viable workaround - for example, if we usedbelongsToManyrelationships instead ofhasMany.MHabashy10 commentedon Dec 30, 2017
Hi @krzysztofczernek am having the same issue did you found a solution or workaround maybe?
Thanks.
rlindgren commentedon Jan 24, 2018
I think the
ORDERandLIMITmust still be applied within the subquery, since the subquery resolves to the outermost scope (projects). In cases whereORDERis determined by an association, theJOINfor that association must also occur within the subquery andORDERshould be applied to the innerSELECTbefore resolving and joining any additional associations (which don't affectORDERandLIMIT. So, in this case, theJOINfororganizationstoauthorsshould also occur inside of the subquery, so that theORDERandLIMITare correct.If there are many
includeassociations, all of them that influence theORDERshould beJOINed inside of the subquery and theORDERandLIMITshould be applied to the subquery before joining the other associations outside of the subquery.I believe that the second generated query that looks like this:
should actually look like this:
does that look right? I'm using
mssqland haven't actually tested forsqliteyet... but formssql, this does the trick for me. I can try to run a test onsqlitein a bit to confirm...This way, it only
JOINreviewsforprojectsthat are actually returned.blaivs commentedon Feb 14, 2018
Had a similar problem and solved by using
subQuery:true. It would look like this:Although using mssql, had to add another order for the main model, as it wasn't generating a valid expression:
yonjah commentedon Mar 16, 2018
@blaisax using
subQuery: falseseem great as a workaround.I'm just not sure what the implications are since I couldn't find any documentation for it.
I assume in some cases some results might get duplicated but I haven't seen it happening for my use case
stale commentedon Jun 14, 2018
OwenLittleWhite commentedon Jan 10, 2020
When there is one-one relation,
subQuery: falseworks, but one-many and many-many, it doesn't work. Anyone solved this problem?FREEZX commentedon Mar 15, 2020
Using subQuery: false breaks the limit + offset parameters in many-to-many relations, because of repeating rows returned by the db for each association
aristov commentedon Mar 24, 2020
subQuery: falsehelps.Leeiio commentedon Apr 14, 2020
not for many-to-many relations
afzalIbnSH commentedon Apr 5, 2021
The below scope breaks when I apply a limit.
It works well without limit i.e.
Foo.scope({ method: ['foobar', [ 'INDIVIDUAL-TYPE-1' ], [ 'TYPE-1', 'TYPE-2' ], 1 ] }).findAndCountAll()but breaks with limit i.e.Foo.scope({ method: ['foobar', [ 'INDIVIDUAL-TYPE-1' ], [ 'TYPE-1', 'TYPE-2' ], 1 ] ] }).findAndCountAll({ limit: 10 })caseyt commentedon Jan 10, 2025
Still open? Any plans to address the biggest issue in this repo that has been reported over and over again? The maintainers of this repo are useless.