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

Commit 8ba8ae91 by pola88 Committed by Jan Aagaard Meier

Fixes setAssociation with scope (#7223)

1 parent ab0570d5
...@@ -572,6 +572,8 @@ BelongsToMany.prototype.injectSetter = function(obj) { ...@@ -572,6 +572,8 @@ BelongsToMany.prototype.injectSetter = function(obj) {
} }
where[identifier] = this.get(sourceKey); where[identifier] = this.get(sourceKey);
_.assign(where, association.through.scope);
return association.through.model.findAll(_.defaults({ return association.through.model.findAll(_.defaults({
where: where, where: where,
raw: true, raw: true,
......
...@@ -616,6 +616,56 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -616,6 +616,56 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
expect(tasks[0].title).to.equal('wat'); expect(tasks[0].title).to.equal('wat');
}); });
}); });
it('using scope to set associations', function() {
var ItemTag = this.sequelize.define('ItemTag', {
id : { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
tag_id: { type: DataTypes.INTEGER, unique: false },
taggable: { type: DataTypes.STRING },
taggable_id: { type: DataTypes.INTEGER, unique: false }
}),
Tag = this.sequelize.define('Tag', {
id : { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: DataTypes.STRING
}),
Comment = this.sequelize.define('Comment', {
id : { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: DataTypes.STRING
}),
Post = this.sequelize.define('Post', {
id : { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: DataTypes.STRING
});
Post.belongsToMany(Tag, {
through: { model: ItemTag, unique: false, scope: { taggable: 'post' } },
foreignKey: 'taggable_id'
});
Comment.belongsToMany(Tag, {
through: { model: ItemTag, unique: false, scope: { taggable: 'comment' } },
foreignKey: 'taggable_id'
});
return this.sequelize.sync({ force: true }).then(function() {
return Promise.all([
Post.create({ name: 'post1' }),
Comment.create({ name: 'comment1' }),
Tag.create({ name: 'tag1' })
]);
}).bind({}).spread(function(post, comment, tag) {
this.post = post;
this.comment = comment;
this.tag = tag;
return this.post.setTags([this.tag]);
}).then(function() {
return this.comment.setTags([this.tag]);
}).then(function() {
return this.comment.getTags();
}).then(function(_tags) {
expect(_tags).to.have.length(1);
});
});
}); });
describe('createAssociations', function() { describe('createAssociations', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!