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

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 {
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;
}
}
......@@ -1312,7 +1312,7 @@ class Model {
if (!currentAttribute) {
await this.QueryInterface.removeColumn(this.getTableName(options), columnName, options);
continue;
}
}
if (currentAttribute.primaryKey) continue;
// Check foreign keys. If it's a foreign key, it should remove constraint first.
const references = currentAttribute.references;
......@@ -3086,7 +3086,7 @@ class Model {
}
valuesUse = values;
// Get instances and run beforeUpdate hook on each record individually
let instances;
let updateDoneRowByRow = false;
......
......@@ -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'), () => {
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,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!