Skip to content

Commit

Permalink
fix(scope): do not assign scope on eagerly loaded associations (#9292)
Browse files Browse the repository at this point in the history
  • Loading branch information
Verdier authored and sushantdhiman committed Apr 13, 2018
1 parent 1b295c0 commit 3b12097
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3503,11 +3503,9 @@ class Model {
value = value[0];
}
isEmpty = value && value[primaryKeyAttribute] === null || value === null;
value = !isEmpty && _.assign(value, association.scope);
this[accessor] = this.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions);
} else {
isEmpty = value[0] && value[0][primaryKeyAttribute] === null;
value = !isEmpty && value.map(v => _.assign(v, association.scope));
this[accessor] = this.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions);
}
}
Expand Down Expand Up @@ -3759,6 +3757,7 @@ class Model {
});
} else {
instance.set(include.association.foreignKey, this.get(include.association.sourceKey || this.constructor.primaryKeyAttribute, {raw: true}));
_.assign(instance, include.association.scope);
return instance.save(includeOptions);
}
});
Expand Down
47 changes: 46 additions & 1 deletion test/integration/associations/scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const chai = require('chai'),
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
Sequelize = require('../../../index'),
Promise = Sequelize.Promise;
Promise = Sequelize.Promise,
Op = Sequelize.Op;

describe(Support.getTestDialectTeaser('associations'), () => {
describe('scope', () => {
Expand All @@ -15,6 +16,7 @@ describe(Support.getTestDialectTeaser('associations'), () => {
this.Question = this.sequelize.define('question', {});
this.Comment = this.sequelize.define('comment', {
title: Sequelize.STRING,
type: Sequelize.STRING,
commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER,
isMain: {
Expand Down Expand Up @@ -43,6 +45,15 @@ describe(Support.getTestDialectTeaser('associations'), () => {
},
constraints: false
});
this.Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
as: 'coloredComments',
scope: {
commentable: 'post',
type: { [Op.in]: ['blue', 'green'] }
},
constraints: false
});
this.Post.hasOne(this.Comment, {
foreignKey: 'commentable_id',
as: 'mainComment',
Expand Down Expand Up @@ -287,6 +298,40 @@ describe(Support.getTestDialectTeaser('associations'), () => {
expect(comment.get('commentable')).to.equal('post');
});
});
it('should include associations with operator scope values', function() {
return this.sequelize.sync({force: true}).then(() => {
return Promise.join(
this.Post.create(),
this.Comment.create({
title: 'I am a blue comment',
type: 'blue'
}),
this.Comment.create({
title: 'I am a red comment',
type: 'red'
}),
this.Comment.create({
title: 'I am a green comment',
type: 'green'
})
);
}).spread((post, commentA, commentB, commentC) => {
this.post = post;
return post.addComments([commentA, commentB, commentC]);
}).then(() => {
return this.Post.findById(this.post.id, {
include: [{
model: this.Comment,
as: 'coloredComments'
}]
});
}).then(post => {
expect(post.coloredComments.length).to.equal(2);
for (const comment of post.coloredComments) {
expect(comment.type).to.match(/blue|green/);
}
});
});
});

if (Support.getTestDialect() !== 'sqlite') {
Expand Down

0 comments on commit 3b12097

Please sign in to comment.