Skip to content

problems with improved findAndCount #2663

Closed

Description

@mithrandirii

i have some problems with the new findAndCount from commit 82403cf

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

Activity

  1. mickhansen commented on Dec 4, 2014

    @mickhansen
    Contributor

    You can either use

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

    Or

    include: [{
      model: Project,
      as: 'Project',
      required: true
    }]
  2. mithrandirii commented on Dec 4, 2014

    @mithrandirii
    Author

    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 ?

  3. mickhansen commented on Dec 4, 2014

    @mickhansen
    Contributor

    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.

  4. yepp4you commented on Dec 7, 2014

    @yepp4you

    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?

  5. mickhansen commented on Dec 7, 2014

    @mickhansen
    Contributor

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

  6. janmeier commented on Apr 27, 2015

    @janmeier
    Member

    No more information gotten, closing

  7. jocull commented on Jan 21, 2016

    @jocull

    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.

  8. jocull commented on Jan 21, 2016

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions