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

Commit 2dcd198b by JacobLey Committed by GitHub

fix(include): separate queries are not sub-queries (#12144)

1 parent ce5bc374
...@@ -547,7 +547,7 @@ class Model { ...@@ -547,7 +547,7 @@ class Model {
include.subQuery = false; include.subQuery = false;
} else { } else {
include.subQueryFilter = false; include.subQueryFilter = false;
include.subQuery = include.subQuery || include.hasParentRequired && include.hasRequired; include.subQuery = include.subQuery || include.hasParentRequired && include.hasRequired && !include.separate;
} }
} }
......
...@@ -477,6 +477,49 @@ if (current.dialect.supports.groupedLimit) { ...@@ -477,6 +477,49 @@ if (current.dialect.supports.groupedLimit) {
}); });
}); });
}); });
it('should work with required non-separate parent and required child', async 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);
await this.sequelize.sync({ force: true });
const task = await Task.create({ id: 1 });
const user = await task.createUser({ id: 2 });
await user.createCompany({ id: 3 });
const results = await 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
}]
}]
}]
}]
});
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);
});
}); });
}); });
} }
...@@ -405,6 +405,26 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -405,6 +405,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(options.include[0].subQueryFilter).to.equal(false); 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() { it('should tag a hasMany association with where', function() {
const options = Sequelize.Model._validateIncludedElements({ const options = Sequelize.Model._validateIncludedElements({
model: this.User, model: this.User,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!