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

Commit c528f6d6 by Cameron Reid Committed by Sushant

fix(association/count): use model field in `count[Association]` (#8227)

1 parent 785383e0
......@@ -444,7 +444,7 @@ class BelongsToMany extends Association {
options = Utils.cloneDeep(options);
options.attributes = [
[sequelize.fn('COUNT', sequelize.col([association.target.name, model.primaryKeyAttribute].join('.'))), 'count']
[sequelize.fn('COUNT', sequelize.col([association.target.name, model.primaryKeyField].join('.'))), 'count']
];
options.joinTableAttributes = [];
options.raw = true;
......
......@@ -95,6 +95,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
foreignKey: 'task_id'
});
this.User.belongsToMany(this.Comment, {
foreignKey: 'userId',
otherKey: 'commentId',
through: 'userComments'
});
return Promise.all([
queryInterface.createTable('users', {
userId: {
......@@ -152,6 +158,14 @@ describe(Support.getTestDialectTeaser('Model'), () => {
updated_at: {
type: DataTypes.DATE
}
}),
queryInterface.createTable('userComments', {
commentId: {
type: DataTypes.INTEGER
},
userId: {
type: DataTypes.INTEGER
}
})
]);
});
......@@ -539,7 +553,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
it('should work with with an belongsTo association getter', function() {
it('should work with a belongsTo association getter', function() {
const userId = Math.floor(Math.random() * 100000);
return Promise.join(
this.User.create({
......@@ -606,6 +620,22 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
it('should work with `belongsToMany` association `count`', function() {
return this.User.create({
name: 'John'
})
.then(user => user.countComments())
.then(commentCount => expect(commentCount).to.equal(0));
});
it('should work with `hasMany` association `count`', function() {
return this.User.create({
name: 'John'
})
.then(user => user.countTasks())
.then(taskCount => expect(taskCount).to.equal(0));
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!