sequelize: an update with .scope doesn't work

What are you doing?

When doing an .update with a scope, the scope options are ignored. Using scope works with find and create.

User.scope('withoutPassword').update({ fields }, ...)


defaultScope: {
     attributes: {
         exclude: ['password']
      }
},
scopes: {
  withoutPassword: {
     attributes: {
        exclude: ['password']
     }
  }
}

What do you expect to happen?

Should respect the scope, or at least the defaultScope

Expected JSON output:

username
first_name

What is actually happening?

Model returns all data values, with scope being ignored

Actual JSON output:

username
password
first_name

Dialect: postgres Dialect version: 10.3 Database version: 14.14 Sequelize version: ^4.38.0 Tested with latest release: Yes (If yes, specify that version) 4.38.0

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

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 9
  • Comments: 16 (3 by maintainers)

Most upvoted comments

Still an issue with 6.29.0. Docs mention that this should work.

Any updates regarding this?

We’re facing the same issue. Any news on this matter?

@jackiewung It’s just some code that reproduce your issue, will help me or someone else later on to quickly test their fix

const Sequelize = require('./index');
const sequelize = require('./test/support').createSequelizeInstance();

const Model = sequelize.define('model', {
  name: Sequelize.STRING,
  value: Sequelize.INTEGER,
  password: Sequelize.STRING
}, {
    defaultScope: {
      attributes: {
        exclude: ['password']
      }
    },
    scopes: {
      withoutPassword: {
        attributes: {
          exclude: ['password']
        }
      }
    }
  });

Model.sync({force: true})
  .then(() => Model.create({
    name: 'Abc',
    value: 1,
    password: 'xyz'
  }))
  .then(() => Model.scope('withoutPassword').update({
    value: 2
  }, {
    where: {
      value: 1
    },
    returning: true
  }))
  .spread((count, r) => {
    console.log(r[0].toJSON());
  });
{ id: 1,
  name: 'Abc',
  value: 2,
  password: 'xyz',
  createdAt: 2018-08-15T05:02:57.863Z,
  updatedAt: 2018-08-15T05:02:57.876Z }