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

Commit f00bd96c by Gareth Flowers Committed by Jan Aagaard Meier

fix(query): does not include primary key when grouping (#8162)

* fix(query): does not include primary key when grouping

When grouping a model that links through a belongsTo association the primaryKey is no longer automatically added to the attributes list.

* test(query): uses stricter type checks for primary key group tests
1 parent 02050875
...@@ -1514,9 +1514,11 @@ class Model { ...@@ -1514,9 +1514,11 @@ class Model {
this._validateIncludedElements(options, tableNames); this._validateIncludedElements(options, tableNames);
// If we're not raw, we have to make sure we include the primary key for deduplication // If we're not raw, we have to make sure we include the primary key for deduplication
if (options.attributes && !options.raw && this.primaryKeyAttribute && options.attributes.indexOf(this.primaryKeyAttribute) === -1) { if (options.attributes && !options.raw && this.primaryKeyAttribute && options.attributes.indexOf(this.primaryKeyAttribute) === -1) {
options.originalAttributes = options.attributes; options.originalAttributes = options.attributes;
options.attributes = [this.primaryKeyAttribute].concat(options.attributes); if (!options.group || !options.hasSingleAssociation || options.hasMultiAssociation) {
options.attributes = [this.primaryKeyAttribute].concat(options.attributes);
}
} }
} }
......
...@@ -51,6 +51,49 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -51,6 +51,49 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(parseInt(posts[1].get('comment_count'))).to.be.equal(2); expect(parseInt(posts[1].get('comment_count'))).to.be.equal(2);
}); });
}); });
it('should not add primary key when grouping using a belongsTo association', () => {
const Post = current.define('Post', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
});
const Comment = current.define('Comment', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
text: { type: DataTypes.STRING, allowNull: false }
});
Post.hasMany(Comment);
Comment.belongsTo(Post);
return current.sync({ force: true }).then(() => {
return Post.bulkCreate([
{ name: 'post-1' },
{ name: 'post-2' }
]);
}).then(() => {
return Comment.bulkCreate([
{ text: 'Market', PostId: 1 },
{ text: 'Text', PostId: 2 },
{ text: 'Abc', PostId: 2 },
{ text: 'Semaphor', PostId: 1 },
{ text: 'Text', PostId: 1 }
]);
}).then(() => {
return Comment.findAll({
attributes: ['PostId', [Sequelize.fn('COUNT', Sequelize.col('Comment.id')), 'comment_count']],
include: [
{ model: Post, attributes: [] }
],
group: ['PostId']
});
}).then(posts => {
expect(posts[0].get().hasOwnProperty('id')).to.equal(false);
expect(posts[1].get().hasOwnProperty('id')).to.equal(false);
expect(parseInt(posts[0].get('comment_count'))).to.be.equal(3);
expect(parseInt(posts[1].get('comment_count'))).to.be.equal(2);
});
});
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!