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

Commit 3b623cbf by Jozef Hartinger Committed by Sushant

fix(query-generator): 1-to-many join in subQuery filter missing where clause (#9228) (#9247)

1 parent 2da9e4df
...@@ -1663,9 +1663,10 @@ const QueryGenerator = { ...@@ -1663,9 +1663,10 @@ const QueryGenerator = {
include: Model._validateIncludedElements(topInclude).include, include: Model._validateIncludedElements(topInclude).include,
model: topInclude.model, model: topInclude.model,
where: { where: {
[Op.and]: [{ [Op.and]: [
[Op.join]: this.sequelize.asIs(join) topInclude.where,
}] { [Op.join]: this.sequelize.asIs(join) }
]
}, },
limit: 1, limit: 1,
tableAs: topInclude.as, tableAs: topInclude.as,
......
...@@ -31,6 +31,7 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -31,6 +31,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
* | * |
* | * |
* | * |
* N
* [Footnote] * [Footnote]
*/ */
beforeEach(function () { beforeEach(function () {
...@@ -360,7 +361,7 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -360,7 +361,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
}); });
/* /*
* one-to-one * one-to-many
*/ */
it('supports required one-to-many association', function () { it('supports required one-to-many association', function () {
return this.sequelize.sync({ force: true }) return this.sequelize.sync({ force: true })
...@@ -391,20 +392,23 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -391,20 +392,23 @@ describe(Support.getTestDialectTeaser('Include'), () => {
return this.sequelize.sync({ force: true }) return this.sequelize.sync({ force: true })
.then(() => Promise.join( .then(() => Promise.join(
this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')), this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')),
this.Comment.bulkCreate(build('comment0', 'comment1')) this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2'))
)) ))
.spread((posts, comments) => Promise.join( .spread((posts, comments) => Promise.join(
posts[0].addComment(comments[0]), posts[0].addComment(comments[0]),
posts[2].addComment(comments[1]) posts[1].addComment(comments[1]),
posts[2].addComment(comments[2])
)) ))
.then(() => this.Post.findAll({ .then(() => this.Post.findAll({
include: [{ include: [{
model: this.Comment, model: this.Comment,
required: true, required: true,
where: { where: {
name: { [Op.or]: [{
[this.sequelize.Op.like]: 'comment%' name: 'comment0'
} }, {
name: 'comment2'
}]
} }
}], }],
order: ['name'], order: ['name'],
...@@ -417,6 +421,31 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -417,6 +421,31 @@ describe(Support.getTestDialectTeaser('Include'), () => {
}); });
}); });
it('supports required one-to-many association with where clause (findOne)', function () {
return this.sequelize.sync({ force: true })
.then(() => Promise.join(
this.Post.bulkCreate(build('alpha', 'bravo', 'charlie')),
this.Comment.bulkCreate(build('comment0', 'comment1', 'comment2'))
))
.spread((posts, comments) => Promise.join(
posts[0].addComment(comments[0]),
posts[1].addComment(comments[1]),
posts[2].addComment(comments[2])
))
.then(() => this.Post.findOne({
include: [{
model: this.Comment,
required: true,
where: {
name: 'comment2'
}
}]
}))
.then(post => {
expect(post.name).to.equal('charlie');
});
});
it('supports 2 levels of required one-to-many associations', function () { it('supports 2 levels of required one-to-many associations', function () {
return this.sequelize.sync({ force: true }) return this.sequelize.sync({ force: true })
.then(() => Promise.join( .then(() => Promise.join(
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!