Skip to content

Commit 1a16b91

Browse files
authored
fix(utils): clone attributes before mutating them (sequelize#13226)
1 parent 39299a6 commit 1a16b91

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ exports.mapOptionFieldNames = mapOptionFieldNames;
186186

187187
function mapWhereFieldNames(attributes, Model) {
188188
if (attributes) {
189+
attributes = cloneDeep(attributes);
189190
getComplexKeys(attributes).forEach(attribute => {
190191
const rawAttribute = Model.rawAttributes[attribute];
191192

test/integration/associations/scope.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe(Support.getTestDialectTeaser('associations'), () => {
1919
commentable: Sequelize.STRING,
2020
commentable_id: Sequelize.INTEGER,
2121
isMain: {
22+
field: 'is_main',
2223
type: Sequelize.BOOLEAN,
2324
defaultValue: false
2425
}
@@ -298,6 +299,11 @@ describe(Support.getTestDialectTeaser('associations'), () => {
298299
expect(comment.type).to.match(/blue|green/);
299300
}
300301
});
302+
it('should not mutate scope when running SELECT query (#12868)', async function() {
303+
await this.sequelize.sync({ force: true });
304+
await this.Post.findOne({ where: {}, include: [{ association: this.Post.associations.mainComment, attributes: ['id'], required: true, where: {} }] });
305+
expect(this.Post.associations.mainComment.scope.isMain).to.equal(true);
306+
});
301307
});
302308

303309
if (Support.getTestDialect() !== 'sqlite') {

0 commit comments

Comments
 (0)