Skip to content

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

@dariomncs

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions