Skip to content

findAndCountAll crashes with a query which runs in findAll #4194

Closed

Description

@slawo

This example will run:

User.findAll({
    where: sequelize.literal('profiles.userId IS null'),
    include: [
    { model: Profile}
    ]
  })

This one will not:

User.findAndCountAll({
    where: sequelize.literal('profiles.userId IS null'),
    include: [
    { model: Profile}
    ]
  })

Activity

  1. janmeier commented on Jul 27, 2015

    @janmeier
    Member

    How will it not run?

  2. slawo commented on Jul 27, 2015

    @slawo
    Author

    @janmeier, when running :

    User.findAndCountAll({
        where: sequelize.literal('profiles.userId IS null'),
        include: [Profile]
      })
    

    I get the following error:

    Executing (default): SELECT count(*) AS `count` FROM `users` AS `user` WHERE profiles.userId IS null;
    Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: profiles.userId
        at Query.formatError (.../node_modules/sequelize/lib/dialects/sqlite/query.js:242:14)
        at .../node_modules/sequelize/lib/dialects/sqlite/query.js:47:29
        at replacement (.../node_modules/sqlite3/lib/trace.js:20:31)
        at Statement.errBack (.../node_modules/sqlite3/lib/sqlite3.js:16:21)
    

    It seems the whole include is ignored during count. As if a different query was ran.

    I have just updated to 3.5
    Before I had something along those lines (sql-string.js:144 error undefined is not a function)

  3. janmeier commented on Jul 27, 2015

    @janmeier
    Member

    That's because we remove non-required includes, in order to make the count query faster.

    You could make the include required: include: [{ model: Profile, required: true}]. But note that this converts the include from a left to inner join

  4. slawo commented on Jul 27, 2015

    @slawo
    Author

    @janmeier So basically any constraints in the include are then ignored and the count is simply false?

    This doesn't sound like an expected behaviour.

  5. janmeier commented on Jul 27, 2015

    @janmeier
    Member

    If there are any constraints directly on the include (include.where) the include is marked as required, and thus not removed. But in this case, there is no way to identify that the include is required.

    http://docs.sequelizejs.com/en/latest/api/model/#findandcountfindoptions-promiseobject

  6. slawo commented on Jul 27, 2015

    @slawo
    Author

    @janmeier, are there other places where this type of optimisation is done?

    Adding a keyword fo force sequelize keep the include could be useful: include: [ { model: Profile, keep:true} ]

    Without changing the type of join that is.

  7. slawo commented on Jul 27, 2015

    @slawo
    Author

    @janmeier Actually a much more elegant solution would be to use the required option like this:

    include: [ { model: Profile, required:'left'} ]
    

    This way there is no new option, and this obviously means we want to force sequelize to use the left join whatever happens.

    What do you think of this idea?

  8. added a commit that references this issue on Jul 27, 2015
    f82aadc
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions