Skip to content

Commit

Permalink
fix(include): separate queries are not sub-queries (#12144)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLey committed Apr 22, 2020
1 parent ce5bc37 commit 2dcd198
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/model.js
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3086,7 +3086,7 @@ class Model {
}

valuesUse = values;

// Get instances and run beforeUpdate hook on each record individually
let instances;
let updateDoneRowByRow = false;
Expand Down
43 changes: 43 additions & 0 deletions test/integration/include/separate.test.js
Expand Up @@ -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);
});
});
});
}
20 changes: 20 additions & 0 deletions test/unit/model/include.test.js
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

0 comments on commit 2dcd198

Please sign in to comment.