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

Commit 59b8a7bf by Vyacheslav Aristov Committed by GitHub

fix(include): check if attributes specified for included through model (#12316)

1 parent 6d87cc58
...@@ -604,7 +604,9 @@ class Model { ...@@ -604,7 +604,9 @@ class Model {
// pseudo include just needed the attribute logic, return // pseudo include just needed the attribute logic, return
if (include._pseudo) { 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); return Utils.mapFinderOptions(include, include.model);
} }
......
...@@ -1835,6 +1835,50 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -1835,6 +1835,50 @@ describe(Support.getTestDialectTeaser('Include'), () => {
expect(parseInt(post.get('commentCount'), 10)).to.equal(3); expect(parseInt(post.get('commentCount'), 10)).to.equal(3);
}); });
it('should ignore include with attributes: [] and through: { attributes: [] } (used for aggregates)', async 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' });
await this.sequelize.sync({ force: true });
await User.create({
name: Math.random().toString(),
projects: [
{ title: Math.random().toString() },
{ title: Math.random().toString() },
{ title: Math.random().toString() }
]
}, {
include: [User.associations.projects]
});
const users = await User.findAll({
attributes: [
[this.sequelize.fn('COUNT', this.sequelize.col('projects.id')), 'projectsCount']
],
include: {
association: User.associations.projects,
attributes: [],
through: { attributes: [] }
},
group: ['User.id']
});
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', async function() { it('should not add primary key when including and aggregating with raw: true', async function() {
const Post = this.sequelize.define('Post', { const Post = this.sequelize.define('Post', {
title: DataTypes.STRING title: DataTypes.STRING
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!