problems with improved findAndCount
i have some problems with the new findAndCount from commit 82403cfc1d165e2a01b6b0170efae6fc0355ba85
if i use an where statement for an included model, it throws an error now, cause the model isn't included any more
example ...
var User = sequelize.define('User', {
name: Sequelize.STRING
});
var Project = sequelize.define('Project', {
title: Sequelize.STRING,
description: Sequelize.TEXT
});
Project.hasMany(User, {
as: 'Workers'
});
User.findAndCount({
include: [{
model: Project,
as: 'Project'
}],
where: {
'Project.title': 'test'
}
}).then(function (result) {
console.log('test', result);
});
You can either use
include: [{
model: Project,
as: 'Project',
where: {
title: 'test'
}
}]
Or
include: [{
model: Project,
as: 'Project',
required: true
}]
my problem is that both results in an inner join, but i need an left join
maybe we could use an option to turn off the new findAndCount feature to exlude the models ?
Perhaps:
include: [{
model: Project,
as: 'Project',
where: {
title: 'test'
},
required: false
}]
Then it will be used to include filtered projects on the main table but won't affect the count.
In the case of that order model is same where model, SQL error is happen in order clause ( ER_BAD_FIELD_ERROR... )
example) select ... from (select ... where a.xxx ... limit 20) ... left outter join .... order by a.xxx desc
How can i resolve?
@yepp4you We need more information than that, not really sure what you're describing here.
No more information gotten, closing
I ran into this issue today. The problem is that when you are using FindAndCountAll with a left join (include with require != true), it is left out of the "count" query. Thus when trying to set where: { 'JoinedTable.column': 'something' } the joined column is absent (because the table is not joined during counting) and the query fails.
Further, using .count(opts) seems to malform:
When calling with a 'where JoinedTable.column' on the parent query:
SELECT `Official`.*
FROM (SELECT Count(`Official`.`id`) AS `count`,
`Official`.`id` AS `id`
FROM `officials` AS `Official`
WHERE `HeadOfficialGames`.`id` IS NULL) AS `Official`
LEFT OUTER JOIN `games` AS `HeadOfficialGames`
ON `Official`.`id` = `HeadOfficialGames`.`headofficialid`;
This errors because in the context of the subquery the joined column is not present.
When calling with 'where column' on the include query:
SELECT `Official`.*
FROM (SELECT Count(`Official`.`id`) AS `count`,
`Official`.`id` AS `id`
FROM `officials` AS `Official`) AS `Official`
LEFT OUTER JOIN `games` AS `HeadOfficialGames`
ON `Official`.`id` = `HeadOfficialGames`.`headofficialid`
AND `HeadOfficialGames`.`headofficialid` IS NULL;
Produces:
"message": "ER_MIX_OF_GROUP_FUNC_AND_FIELDS: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'eventlink.Official.id'; this is incompatible with sql_mode=only_full_group_by"
In short, there's no way to pull of a "where relationship does not exist" in a count query.