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

Commit f3d54bf2 by Mick Hansen

fix(include): include.attributes set to empty array will no longer force primary key inclusion

1 parent 14ee54f4
# Next
- [CHANGED] The `references` property of model attributes has been transformed to an object: `{type: Sequelize.INTEGER, references: { model: SomeModel, key: 'some_key' }}`. The former format is deprecated.
- [FIXED] `include.attributes = []` will no longer force the inclusion of the primary key, making it possible to write aggregates with includes.
- [CHANGED] The `references` property of model attributes has been transformed to an object: `{type: Sequelize.INTEGER, references: { model: SomeModel, key: 'some_key' }}`. The former format (`references` and `referecesKey`) still exists but is deprecated and will be removed in 4.0.
# 3.0.0
......
......@@ -2049,11 +2049,14 @@ module.exports = (function() {
if (include.attributes) {
include.originalAttributes = include.attributes.slice(0);
include.model.primaryKeyAttributes.forEach(function(attr) {
if (include.attributes.indexOf(attr) === -1) {
include.attributes.unshift(attr);
}
});
if (include.attributes.length) {
include.model.primaryKeyAttributes.forEach(function(attr) {
if (include.attributes.indexOf(attr) === -1) {
include.attributes.unshift(attr);
}
});
}
} else {
include.attributes = Object.keys(include.model.tableAttributes);
}
......
......@@ -1877,5 +1877,46 @@ describe(Support.getTestDialectTeaser('Include'), function() {
});
});
});
it('should ignore include with attributes: [] (used for aggregates)', function () {
var Post = this.sequelize.define('Post', {
title: DataTypes.STRING
})
, Comment = this.sequelize.define('Comment', {
content: DataTypes.TEXT
});
Post.Comments = Post.hasMany(Comment, {as: 'comments'});
return this.sequelize.sync({force: true}).bind(this).then(function () {
return Post.create({
title: Math.random().toString(),
comments: [
{content: Math.random().toString()},
{content: Math.random().toString()},
{content: Math.random().toString()},
]
}, {
include: [Post.Comments]
});
}).then(function () {
return Post.findAll({
attributes: [
'title',
[this.sequelize.fn('COUNT', this.sequelize.col('comments.id')), 'commentCount']
],
include: [
{association: Post.Comments, attributes: []}
]
});
}).then(function (posts) {
expect(posts.length).to.equal(1);
var post = posts[0];
expect(post.get('comments')).not.to.be.ok;
expect(post.get('commentCount')).to.equal(3);
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!