Repository navigation
Closed
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
Hi,
Sequelize version 1.7
I think there is a problem with the query generation when there is a combinaison of INNER JOIN and LEFT OUTER JOIN with LIMIT
Here the illustration:
var options = {
include: [ {model: db.User, as: 'Responsible', attributes: ['username'], required: true},
{model: db.Application, as: 'Application', attributes: ['name'], required: true},
{model: db.Subtask, as: 'Subtasks', attributes: ['ended', 'title']},
{model: db.State, as: 'State', attributes: ['title','type'], where: {isTerminate: true}, required: true}],
offset: db.limit*(req.params.pageid-1),
limit: db.limit,
order: [ [db.sequelize.fn('datetime', db.sequelize.col('Tasks.endedAt')), 'DESC'],
// 2 Lines below are bugged
//[{model: db.Application, as: 'Application'}, 'name']
//[db.sequelize.fn('lower',db.sequelize.col('Subtasks.title'))]
]
}
db.Task.findAndCountAll(options).success(function(result) {...});Here the query
SELECT
`Tasks`.*, `Subtasks`.`ended` AS `Subtasks.ended`,
`Subtasks`.`title` AS `Subtasks.title`,
`Subtasks`.`id` AS `Subtasks.id`
FROM
(SELECT
`Tasks`.*, `Responsible`.`username` AS `Responsible.username`,
`Responsible`.`id` AS `Responsible.id`,
`Application`.`nom` AS `Application.name`,
`Application`.`id` AS `Application.id`,
`State`.`title` AS `State.title`,
`State`.`type` AS `State.type`,
`State`.`id` AS `State.id`
FROM `Tasks`
INNER JOIN `Users` AS `Responsible` ON `Responsible`.`id` = `Tasks`.`UserId`
INNER JOIN `Applications` AS `Application` ON `Application`.`id` = `Tasks`.`ApplicationId`
INNER JOIN `States` AS `State`
ON `State`.`id` = `Tasks`.`StateId`
AND `State`.`isTerminate`=1
ORDER BY datetime(`Tasks`.`endedAt`) DESC, lower(`Subtasks`.`title`) LIMIT 20
) AS `Tasks`
LEFT OUTER JOIN `Subtasks` AS `Subtasks`
ON `Tasks`.`id` = `Subtasks`.`TaskId`
ORDER BY datetime(`Tasks`.`endedAt`) DESC, `Application`.`name`, lower(`Subtasks`.`title`);we can see how SELECT .. FROM and in the second Subtasks are not yet selected
EDIT The expected query : not really see below comments
SELECT
`Tasks`.*, `Responsible`.`username` AS `Responsible.username`,
`Responsible`.`id` AS `Responsible.id`,
`Application`.`name` AS `Application.name`,
`Application`.`id` AS `Application.id`,
`State`.`title` AS `State.title`,
`State`.`type` AS `State.type`,
`State`.`id` AS `State.id`
FROM `Tasks`
INNER JOIN `Users` AS `Responsible` ON `Responsible`.`id` = `Tasks`.`UserId`
INNER JOIN `Applications` AS `Application` ON `Application`.`id` = `Tasks`.`ApplicationId`
INNER JOIN `States` AS `State`
ON `State`.`id` = `Tasks`.`StateId`
AND `State`.`isTerminate`=1
LEFT OUTER JOIN `Subtasks` AS `Subtasks` ON `Tasks`.`id` = `Subtasks`.`TaskId`
ORDER BY datetime(`Tasks`.`datefin`) DESC, `Application`.`name`, lower(`Subtasks`.`title`)
LIMIT 20;Below the SQL error
no such column: Subtasks.title
That's why i think there are troubles in query generation.
Can you fix it ?
Thanks
Metadata
Metadata
Assignees
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Activity
janmeier commentedon Aug 13, 2014
Could you please post your models and associations
ThePoloQ commentedon Aug 13, 2014
Thanks for your reply
Below my models
To help you without limit it works fine, example:
The generated query :
redexp commentedon Aug 13, 2014
Same problem
If I set
limitoption then I expect that limit will be set in the end of sql query, but sudenly it creates subQuery with limit in it. It heppens when I havehasManyrelation likeCompnay.hasMany(Client)and I make query with includedClientand withlimitlikethis creates sql like
but I expected
ThePoloQ commentedon Aug 18, 2014
It is a bit crippling. Have you got any possible solution ?
janmeier commentedon Aug 18, 2014
@redexp @mickhansen knows this stuff better than me, but I'm pretty sure that query is correct, in that it will limit you to 10 companies. If you place the limit at the end of the query it will limit the number of joined rows instead. Suppose company 42 has 3 clients, 1, 2 and 3. That will generate the following joined rows:
That means that company will be counted 3 times in the limit. Therefore we find 10 matching companies first (with the inner query) and then join them with the matching clients
ThePoloQ commentedon Aug 18, 2014
The problem here, i think, it is the ORDER BY. It tried to order in the second select with entities from the first select (left join). And INNER + LEFT have to generate two selects, it is correct for me.
As you said the LIMIT after INNER JOINs it is correct too.
Do you agree ?
janmeier commentedon Aug 18, 2014
Have you tried putting the order for subtask.title inside the include?
ThePoloQ commentedon Aug 18, 2014
For me it will be ignored. Because when i read API, you can't do that.
https://github.com/sequelize/sequelize/wiki/API-Reference-Model#findAll
I tried and it is ignored.
mickhansen commentedon Aug 18, 2014
There is no order support for include (as of yet, limit/order support is on the list though). Placing limit on the outer query for :M row duplicating queries is obviously not possible (and not sure why people would expect that).
Not all cases are supported unfortuneatly, we're constantly working on improving that. Pull requests with test cases are always welcome, fixes evenmore so.
ThePoloQ commentedon Aug 18, 2014
Yes I understand for the LIMIT, and as i said this part is correctly generated.
Can you help me saying in which part of sequelize library i have to search ? I can try a fix for multiple ORDER with combination of INNER and OUTER
[-]Combinaison of INNER JOIN and LEFT OUTER JOIN with LIMIT[/-][+]Combination of INNER JOIN and LEFT OUTER JOIN with LIMIT[/+][-]Combination of INNER JOIN and LEFT OUTER JOIN with LIMIT[/-][+]Combination of INNER JOIN and LEFT OUTER JOIN with LIMIT (ORDER BY)[/+]mickhansen commentedon Aug 18, 2014
https://github.com/sequelize/sequelize/blob/master/lib/dialects/abstract/query-generator.js#L686
This is the method responsible for generating the select query.
Even a PR with a simplified test case covering the issue/case would be helpfull :)
mickhansen commentedon Aug 18, 2014
But yeah most likely it won't work correctly with subqueries and multiple orders (where some of those orders are on included tables).
I'm not even sure how a query like that would look.
ThePoloQ commentedon Aug 19, 2014
I'm not a SQL expert but I think the correct query with order is :
@mickhansen Do you agree with ?
EDIT
22 remaining items
felixfbecker commentedon Oct 6, 2016
Then we should close the issue and remove the bug label?
smfoote commentedon Feb 25, 2017
I hope this will be helpful to someone. I was having the same issue as the original issue, but using v2.4. My query looked like this:
The error for me was in the
orderstatement. I changed it from'date DESC'to[ [ 'date', 'DESC' ] ], and then the ordering began to work correctly. The docs warn against using the string version, but I didn't read them carefully enough.rajajlidi commentedon Apr 11, 2017
i succeed in doing one inner join but when i want to to do double it don't work are there any help?
stale commentedon Jun 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
overlookmotel commentedon Jun 29, 2017
Still may be an issue I think, Mr Robot.
stale commentedon Aug 28, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
abCods commentedon Sep 22, 2017
I am still facing the issue with following scenario.
Client and InternalInquiry has 1:m relationship.
When I include InternalInquiry as Join Table in where clause and put a limit to it. Sequelize produces sub-query due to limit which causes join table's column not being recognized.
Here is my query.
This is the generated SQL
And the error I get is
Unknown column 'InternalInquiry.object_id' in 'where clause'abCods commentedon Sep 22, 2017
I believe the issue has been closed but we know it persists and it is very critical for us as we are evaluating ORMs for our project and we don't want to rework the APIs.
cuxs commentedon Jan 22, 2018
This is still an issue! 😞
This code
Returns this:
As you can see the number of results differs of the actual results that are being returned. And this is the generated query:
That it's ok
But then this:
I don't know what I am doing wrong
miradnan commentedon Jun 29, 2021
I am facing this issue. It works when I create a primary key field. But not without primary key.