From 5c733ef79d00351033b71e6955af81379616fca4 Mon Sep 17 00:00:00 2001 From: Vyacheslav Aristov Date: Tue, 26 May 2020 17:02:31 +0300 Subject: [PATCH] fix(include): check if attributes specified for included through model (#12020) --- lib/model.js | 4 ++- test/integration/include/findAll.test.js | 44 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index e001941848d7..a4d957bcc1b2 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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); } diff --git a/test/integration/include/findAll.test.js b/test/integration/include/findAll.test.js index 6399b115565b..10d5b2f1e7d5 100644 --- a/test/integration/include/findAll.test.js +++ b/test/integration/include/findAll.test.js @@ -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