sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

problems with improved findAndCount

Open mithrandirii opened this issue 11 years ago • 8 comments

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);
});

mithrandirii avatar Dec 04 '14 09:12 mithrandirii

You can either use

include: [{
  model: Project,
  as: 'Project',
  where: {
    title: 'test'
  }
}]

Or

include: [{
  model: Project,
  as: 'Project',
  required: true
}]

mickhansen avatar Dec 04 '14 10:12 mickhansen

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 ?

mithrandirii avatar Dec 04 '14 10:12 mithrandirii

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.

mickhansen avatar Dec 04 '14 11:12 mickhansen

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 avatar Dec 07 '14 08:12 yepp4you

@yepp4you We need more information than that, not really sure what you're describing here.

mickhansen avatar Dec 07 '14 13:12 mickhansen

No more information gotten, closing

janmeier avatar Apr 27 '15 19:04 janmeier

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.

jocull avatar Jan 21 '16 14:01 jocull

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.

jocull avatar Jan 21 '16 14:01 jocull