Skip to content

Scope attribute merge returns different results in findAll() and count() #9787

Closed

Description

@dariomncs

What are you doing?

When using a model with mapped fields eg:

const Foo = sequelize.define('Foo', {
    fooId: {
      field: 'foo_id', // mapped
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    statusId: {
      field: 'status_id', // mapped
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    }
...

and a scope:

Foo.addScope('defaultScope', {
      where: {
        statusId: { [sequelize.Op.ne]: 0 },
      }
})

executing find queries and count have different results:

Foo.findAll({ where: { statusId: 0 } }) // generate sql: Foo.statusId = 0 
Foo.count({ where: { statusId: 0 } }) // generate sql: Foo.statusId != 0

Looking into the code look like that injecting the scope after calling Utils.mapOptionFieldNames(options, this) twice, before injecting the scope causes the issue.

Dialect: any
Sequelize version: 4.38.0
Tested with latest release: yes

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. dariomncs commented on Aug 10, 2018

    @dariomncs
    Author

    suggested fix: #9788

  2. added
    type: bug
    on Aug 15, 2018
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: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions