Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Separate queries are not subQueries #12152

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ class Model {
include.subQuery = false;
} else {
include.subQueryFilter = false;
include.subQuery = include.subQuery || include.hasParentRequired && include.hasRequired;
include.subQuery = include.subQuery || include.hasParentRequired && include.hasRequired && !include.separate;
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/integration/include/separate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,53 @@ if (current.dialect.supports.groupedLimit) {
});
});
});

it('should work with required non-separate parent and required child', function() {
const User = this.sequelize.define('User', {});
const Task = this.sequelize.define('Task', {});
const Company = this.sequelize.define('Company', {});

Task.User = Task.belongsTo(User);
User.Tasks = User.hasMany(Task);
User.Company = User.belongsTo(Company);

return this.sequelize.sync({ force: true }).then(() => {
return Task.create({ id: 1 }).then(task => {
return task.createUser({ id: 2 });
}).then(user => {
return user.createCompany({ id: 3 });
}).then(() => {

return Task.findAll({
include: [{
association: Task.User,
required: true,
include: [{
association: User.Tasks,
attributes: ['UserId'],
separate: true,
include: [{
association: Task.User,
attributes: ['id'],
required: true,
include: [{
association: User.Company
}]
}]
}]
}]
});
}).then(results => {

expect(results.length).to.equal(1);
expect(results[0].id).to.equal(1);
expect(results[0].User.id).to.equal(2);
expect(results[0].User.Tasks.length).to.equal(1);
expect(results[0].User.Tasks[0].User.id).to.equal(2);
expect(results[0].User.Tasks[0].User.Company.id).to.equal(3);
});
});
});
});
});
}
20 changes: 20 additions & 0 deletions test/unit/model/include.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(options.include[0].subQueryFilter).to.equal(false);
});

it('should not tag a separate hasMany association with subQuery true', function() {
const options = Sequelize.Model._validateIncludedElements({
model: this.Company,
include: [
{
association: this.Company.Employees,
separate: true,
include: [
{ association: this.User.Tasks, required: true }
]
}
],
required: true
});

expect(options.subQuery).to.equal(false);
expect(options.include[0].subQuery).to.equal(false);
expect(options.include[0].subQueryFilter).to.equal(false);
});

it('should tag a hasMany association with where', function() {
const options = Sequelize.Model._validateIncludedElements({
model: this.User,
Expand Down