不要怂,就是干,撸起袖子干!

Commit 969978d6 by Jacob Robertson Committed by Sushant

fix(scope): include scope on eagerly created associations (#9127)

1 parent 23652061
......@@ -3459,11 +3459,12 @@ class Model {
if (Array.isArray(value)) {
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);
}
}
......
......@@ -137,6 +137,24 @@ describe(Support.getTestDialectTeaser('associations'), () => {
expect(mainComment.get('title')).to.equal('I am a future main comment');
});
});
it('should create included association with scope values', function() {
return this.sequelize.sync({force: true}).then(() => {
return this.Post.create({
mainComment: {
title: 'I am a main comment created with a post'
}
}, {
include: [{model: this.Comment, as: 'mainComment'}]
});
}).then(post => {
expect(post.mainComment.get('commentable')).to.equal('post');
expect(post.mainComment.get('isMain')).to.be.true;
return this.Post.scope('withMainComment').findById(post.id);
}).then(post => {
expect(post.mainComment.get('commentable')).to.equal('post');
expect(post.mainComment.get('isMain')).to.be.true;
});
});
});
describe('1:M', () => {
......@@ -245,6 +263,30 @@ describe(Support.getTestDialectTeaser('associations'), () => {
expect(logs[0]).to.equal(logs[1]);
});
});
it('should created included association with scope values', function() {
return this.sequelize.sync({force: true}).then(() => {
return this.Post.create({
comments: [{
title: 'I am a comment created with a post'
}, {
title: 'I am a second comment created with a post'
}]
}, {
include: [{model: this.Comment, as: 'comments'}]
});
}).then(post => {
this.post = post;
return post.comments;
}).each(comment => {
expect(comment.get('commentable')).to.equal('post');
}).then(() => {
return this.Post.scope('withComments').findById(this.post.id);
}).then(post => {
return post.getComments();
}).each(comment => {
expect(comment.get('commentable')).to.equal('post');
});
});
});
if (Support.getTestDialect() !== 'sqlite') {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!