Repository navigation
Closed
Description
Hello,
I've come across a bug with query generation which include limiting results with including belongsToMany relation containing WHERE clause.
I made executable script to demonstrate this behavior as they are cases when generated query might return expected result if query and data in database are in specific state.
if you investigate the file the following are queries generated for particular demonstration cases:
"Test 1": Works
Student.findAndCount({
offset: 6,
order: [["id", "DESC"]],
include: [
{
model: Teacher,
as: 'teachers',
where: {
id: {$in: [3, 2]}
}
}
]
});SELECT "student"."id", "student"."age", "student"."name",
"student"."created_at", "student"."updated_at", "teachers"."id" AS
"teachers.id", "teachers"."age" AS "teachers.age", "teachers"."name" AS
"teachers.name", "teachers"."created_at" AS "teachers.created_at",
"teachers"."updated_at" AS "teachers.updated_at",
"teachers.student_teacher"."id" AS "teachers.student_teacher.id",
"teachers.student_teacher"."teacher_id" AS
"teachers.student_teacher.teacher_id", "teachers.student_teacher"."student_id"
AS "teachers.student_teacher.student_id"
FROM "student" AS "student"
INNER JOIN (
"student_teacher" AS "teachers.student_teacher"
INNER JOIN "teacher" AS "teachers"
ON "teachers"."id" = "teachers.student_teacher"."teacher_id"
)
ON "student"."id" = "teachers.student_teacher"."student_id"
AND "teachers"."id" IN (3, 2)
ORDER BY "student"."id" DESC OFFSET 6;"Test 2": Bug - Wrong query generated... but this case returns expected result by "coincidence"
Student.findAndCount({
limit: 5,
offset: 6,
order: [["id", "DESC"]],
include: [
{
model: Teacher,
as: 'teachers',
where: {
id: {$in: [3, 2]}
}
}
]
});SELECT "student".*, "teachers"."id" AS "teachers.id", "teachers"."age" AS
"teachers.age", "teachers"."name" AS "teachers.name", "teachers"."created_at"
AS "teachers.created_at", "teachers"."updated_at" AS "teachers.updated_at",
"teachers.student_teacher"."id" AS "teachers.student_teacher.id",
"teachers.student_teacher"."teacher_id" AS
"teachers.student_teacher.teacher_id", "teachers.student_teacher"."student_id"
AS "teachers.student_teacher.student_id"
FROM (
SELECT "student"."id", "student"."age", "student"."name",
"student"."created_at", "student"."updated_at"
FROM "student" AS "student"
WHERE (
SELECT "student_teacher"."id"
FROM "student_teacher" AS "student_teacher"
INNER JOIN "teacher" AS "teacher"
ON "student_teacher"."teacher_id" = "teacher"."id"
WHERE ("student"."id" = "student_teacher"."student_id")
LIMIT 1
) IS NOT NULL
ORDER BY "student"."id" DESC LIMIT 5 OFFSET 6
) AS "student"
INNER JOIN (
"student_teacher" AS "teachers.student_teacher"
INNER JOIN "teacher" AS "teachers"
ON "teachers"."id" = "teachers.student_teacher"."teacher_id"
)
ON "student"."id" = "teachers.student_teacher"."student_id"
AND "teachers"."id" IN (3, 2)
ORDER BY "student"."id" DESC;"Test 3": Bug - Wrong query - returns unexpected results
Student.findAndCount({
limit: 5,
offset: 6,
include: [
{
model: Teacher,
as: 'teachers',
where: {
id: {$in: [3, 2]}
}
}
]
});SELECT "student".*, "teachers"."id" AS "teachers.id", "teachers"."age" AS
"teachers.age", "teachers"."name" AS "teachers.name", "teachers"."created_at"
AS "teachers.created_at", "teachers"."updated_at" AS "teachers.updated_at",
"teachers.student_teacher"."id" AS "teachers.student_teacher.id",
"teachers.student_teacher"."teacher_id" AS
"teachers.student_teacher.teacher_id", "teachers.student_teacher"."student_id"
AS "teachers.student_teacher.student_id"
FROM (
SELECT "student"."id", "student"."age", "student"."name",
"student"."created_at", "student"."updated_at"
FROM "student" AS "student"
WHERE (
SELECT "student_teacher"."id"
FROM "student_teacher" AS "student_teacher"
INNER JOIN "teacher" AS "teacher"
ON "student_teacher"."teacher_id" = "teacher"."id"
WHERE ("student"."id" = "student_teacher"."student_id")
LIMIT 1
) IS NOT NULL LIMIT 5 OFFSET 6
) AS "student"
INNER JOIN (
"student_teacher" AS "teachers.student_teacher"
INNER JOIN "teacher" AS "teachers"
ON "teachers"."id" = "teachers.student_teacher"."teacher_id"
)
ON "student"."id" = "teachers.student_teacher"."student_id"
AND "teachers"."id" IN (3, 2);
As I look at queries it seems it would be great if generated queries for test cases 2 and 3 would be the same as in 1 with LIMIT included at the end of query, wouldn't it?
Thank you for your work!
Reacted by Drazen Bjelovuk and alixanyan
Activity
mickhansen commentedon Oct 23, 2015
Does it work
findAllrather thanfindAndCount?The
limitis the culprit here since it triggers subquery logic.fogine commentedon Oct 23, 2015
No, It seems it behaves the same way. Is it working for you with
findAll?It rather looks like that
LIMITis causing subquery generation...mickhansen commentedon Oct 23, 2015
Haven't tested it yet, just wanted to make sure that the method was not a factor.
Unfortunately
limitdoesn't work in all cases with includes because of cartesian products.fogine commentedon Oct 23, 2015
well, do you mean it might be problem to generate appropriate sql query in this case? Could you be more specific in which cases with cross-joins it's not currently possible, please?
It really looks like the problem is caused by
LIMIThere. In this case use of subquery is not neccesary, is it?mickhansen commentedon Oct 25, 2015
The limit is necessary when you include an association that result in a cartesian product, which would be the case for a belongsToMany association.
limit for includes is not supported in all cases as i said, but it would. In this case it looks like the
wherestatement is missing from the nested filter subquery, which is highly odd.It would be helpfull if you could provide a SSCCE - Preferably as a PR with an integration test.
fogine commentedon Oct 25, 2015
I'm not sure you've noticed that I've made a SSCCE:
https://gist.github.com/fogine/acb637ec1b4ab2ef5d81
I think it should demonstrate the problem pretty well.
I can make PR with an integration test though.
mickhansen commentedon Oct 26, 2015
@fogine Ah no i missed that. That looks pretty copy/pasteable into a test, so no need to provide a PR.
markdboyd commentedon Jan 6, 2016
Any progress here? I'm fairly sure that I'm running into a similar situation, where I'm using limit and a join that I need isn't being provided on the subquery
mickhansen commentedon Jan 7, 2016
@markdboyd Not much, overall we're working on simplifying this by running associations in seperate queries.
However if you could provide a SSCCE for your case i'd be happy to take a look.
markdboyd commentedon Jan 7, 2016
Basically, my problem is that I have a where clause on the subquery (which is triggered by a limit), but it it isn't working because it references a table that is joined outside the subquery. I'm not sure how much detail to provide for a SSCCE, but here goes: https://gist.github.com/markdboyd/cf19937dec07b1af2bd7
It also seems like #4737 is a related issue here.
mickhansen commentedon Jan 8, 2016
Because of your custom where that is played inside the subquery, don't have a workaround for a planned fix for that at the moment.
markdboyd commentedon Jan 8, 2016
Just so I understand, are you saying that the custom where condition itself is the problem? I was under the impression the problem was the table that needs to be joined for the custom where condition (in the subquery) is only joined on the outer query, not the subquery.
mickhansen commentedon Jan 8, 2016
It gets places inside the subquery cause we wouldn't know not to.
dmgallardo commentedon Mar 10, 2016
Hi, Im having the same issue.
Any progress on this topic.
ybrodsky commentedon Aug 3, 2016
Same issue here. Any progress or workaround?
Is there any reason for those subqueries being there?
janmeier commentedon Aug 3, 2016
@demege @ybrodsky More test cases are always welcome
ybrodsky commentedon Aug 3, 2016
@janmeier my case is exactly the same as @fogine first post.
I have noticed that adding the option
subQuery: falseseems to produce the desired mysql query. Is there any downside of using this?janmeier commentedon Aug 3, 2016
When adding offset and limit to a duplicating (hasMany or belongsToMany relations) you need a subquery - otherwise the offset and limit is applied to the cartesian product, and not the the main table
ybrodsky commentedon Aug 3, 2016
Shouldnt it be applied to the cartesian product if it's an inner join?
My case:
Produced query:
ybrodsky commentedon Aug 4, 2016
I have tested further and noted two things:
ORDERclause inside the subquery. So the results in the subquery are messed up to start with.Testing with raw mysql, if you add the
orderclause inside the subquery and (if any) the join conditions inside the subquery, the results are as expected.Example query which returns expected results when using limit and a condition for the joined model.
Alexsey commentedon Jun 9, 2017
My issue #7344 is looks like related. And adding of
subQuery: falseis looks like a fix for my case as wellstale commentedon Aug 12, 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 🙂
brianemoore57 commentedon Jan 8, 2019
I ran into this because of the deletedAt issue. I want to use userName column in my User table, and as a soft-delete, preserve the option to re-use the userName if first record deleted. I had to change to a exclude constraint to get this behaviour. Then, sequelize.model inserted this options.limit since logic adds it unless a Pk, or a unique column. It doesnt see my exclude constraint as a unique. I know the deletedAt is changed to a boolean flag, so I will be able to use a regular multi-column unique constraint. However, this logic here seems to just generate wrong SQL, at least for postgresql, limiting its use.