New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include generates subqueries #1719
Comments
Holy lack of formatting batman. |
Use |
Hmm, ignore my previous comment - With the code you are showing you shouldn't be getting a subquery, are you sure you are showing the whole thing? Also please test against the latest version + master when reporting an issue, it might already be fixed. |
thanks for the quick reply and sorry about the formatting, however I tried to use required: false previously and it resulted in the same issue. I also tried master branch a sec ago and same problem occurs. To simplify my example:
And my model definition is quite simple too:
this all results in:
|
Btw. I cannot add the where clause to the include, since it would join only on found aliases. |
You are getting the subquery because of the combination of a LIMIT and a :M include. There's no way around this, a subquery is absolutely required. Unfortuneatly there's no way to tell sequelize that your where condition should be outside the subquery. Why can't you put the where on the include? You are querying on the joined table, thats what include.where is there for. Have you tried something like: model.Person.findAll({
include: {
model: model.Alias,
as: 'aliases',
attributes: ['name'],
required: false,
where: {
name: {
like: terms[0]+"%"
}
}
}
limit: limit
}); |
Ok thanks a bunch, skipping the limit does exactly what I want. The join where doesn't work since I need "or" logic there. You guys saved my day! :) |
@mendrik yeah .or() is a complicated case - we need a way users can tell sequelize whether to place the where on the inner or outer query, not sure how to do that yet though (api wise). |
@mickhansen Hey there, I just encountered an issue where I am including a model with its own So, like the poster here, I need to have the subquery logic have TL;DR, you mention here: "we need a way users can tell sequelize whether to place the where on the inner or outer query," has that been done yet? |
@ralusek You can use User.findAll({
include: [Task],
where: {
'$task.name$': 'foobar'
}
}); SELECT *
FROM user
LEFT JOIN task .... ON ..
WHERE task.name = 'foobar' |
@janmeier Thanks. Yea that's actually what I am doing, but I had the logic of my query on what would be the |
Hello, i am also having the same problem but when i use '$' operator to reference columns from a joined table what happens is that, i get an error because the subquery doesnt include the referenced table. Here is my query: The strange thing here is that i have another model, where i user both inner and outer joins, and i never get subquery. I can not figure out what is the difference between the two. |
I ended up using |
|
Hi,
I'm trying to use .findAll with a condition over an associated model:
model.Person.findAll({
include: [{ model: model.Alias, as: 'aliases', attributes: ['name']}],
where: model.sequelize.and(
model.sequelize.or.apply(this, search),
{published: true}
),
limit: limit
}
with "search" being among other person restrictions: ["'aliases.name' like ?", term+"%"]
However this generates:
SELECT
people
.,aliases
.name
ASaliases.name
,aliases
.id
ASaliases.id
FROM (SELECTpeople
. FROMpeople
WHERE ((firstname like 'eva g%' OR surname like 'eva g%' OR concat(firstname,' ',surname) like 'eva g%' OR 'aliases.name' like 'eva g%') ANDpeople
.published
=true) LIMIT 4) ASpeople
LEFT OUTER JOINalias
ASaliases
ONpeople
.id
=aliases
.person_id
;Is it somehow possible to get it so, that there would be no subquery and the join is on the outer clause so the alias condition is applied correctly?
I'm not sure if I'm abusing "include" here, since I'm not really interested in the aliases data, just needed to find the correct people.
Cheers,
Andreas
P.S: I'm using 2.0.0-dev9
The text was updated successfully, but these errors were encountered: