Skip to content

Commit

Permalink
fix(include): check if attributes specified for included through model (
Browse files Browse the repository at this point in the history
  • Loading branch information
aristov committed May 26, 2020
1 parent 7fdc2dc commit 5c733ef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/model.js
Expand Up @@ -611,7 +611,9 @@ class Model {

// pseudo include just needed the attribute logic, return
if (include._pseudo) {
include.attributes = Object.keys(include.model.tableAttributes);
if (!include.attributes) {
include.attributes = Object.keys(include.model.tableAttributes);
}
return Utils.mapFinderOptions(include, include.model);
}

Expand Down
44 changes: 44 additions & 0 deletions test/integration/include/findAll.test.js
Expand Up @@ -1911,6 +1911,50 @@ describe(Support.getTestDialectTeaser('Include'), () => {
});
});

it('should ignore include with attributes: [] and through: { attributes: [] } (used for aggregates)', function() {
const User = this.sequelize.define('User', {
name: DataTypes.STRING
});
const Project = this.sequelize.define('Project', {
title: DataTypes.STRING
});

User.belongsToMany(Project, { as: 'projects', through: 'UserProject' });
Project.belongsToMany(User, { as: 'users', through: 'UserProject' });

return this.sequelize.sync({ force: true }).then(() => {
return User.create({
name: Math.random().toString(),
projects: [
{ title: Math.random().toString() },
{ title: Math.random().toString() },
{ title: Math.random().toString() }
]
}, {
include: [User.associations.projects]
});
}).then(() => {
return User.findAll({
attributes: [
[this.sequelize.fn('COUNT', this.sequelize.col('projects.id')), 'projectsCount']
],
include: {
association: User.associations.projects,
attributes: [],
through: { attributes: [] }
},
group: ['User.id']
});
}).then(users => {
expect(users.length).to.equal(1);

const user = users[0];

expect(user.projects).not.to.be.ok;
expect(parseInt(user.get('projectsCount'), 10)).to.equal(3);
});
});

it('should not add primary key when including and aggregating with raw: true', function() {
const Post = this.sequelize.define('Post', {
title: DataTypes.STRING
Expand Down

0 comments on commit 5c733ef

Please sign in to comment.