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

Commit d5e4a10f by Sushant Committed by GitHub

fix: unknown column with subquery joins (#11404)

1 parent 2172333c
...@@ -1186,10 +1186,10 @@ class QueryGenerator { ...@@ -1186,10 +1186,10 @@ class QueryGenerator {
mainJoinQueries = mainJoinQueries.concat(joinQueries.mainQuery); mainJoinQueries = mainJoinQueries.concat(joinQueries.mainQuery);
if (joinQueries.attributes.main.length > 0) { if (joinQueries.attributes.main.length > 0) {
attributes.main = attributes.main.concat(joinQueries.attributes.main); attributes.main = _.uniq(attributes.main.concat(joinQueries.attributes.main));
} }
if (joinQueries.attributes.subQuery.length > 0) { if (joinQueries.attributes.subQuery.length > 0) {
attributes.subQuery = attributes.subQuery.concat(joinQueries.attributes.subQuery); attributes.subQuery = _.uniq(attributes.subQuery.concat(joinQueries.attributes.subQuery));
} }
} }
} }
...@@ -1671,6 +1671,7 @@ class QueryGenerator { ...@@ -1671,6 +1671,7 @@ class QueryGenerator {
else asRight = `${asLeft}->${asRight}`; else asRight = `${asLeft}->${asRight}`;
let joinOn = `${this.quoteTable(asLeft)}.${this.quoteIdentifier(fieldLeft)}`; let joinOn = `${this.quoteTable(asLeft)}.${this.quoteIdentifier(fieldLeft)}`;
const subqueryAttributes = [];
if (topLevelInfo.options.groupedLimit && parentIsTop || topLevelInfo.subQuery && include.parent.subQuery && !include.subQuery) { if (topLevelInfo.options.groupedLimit && parentIsTop || topLevelInfo.subQuery && include.parent.subQuery && !include.subQuery) {
if (parentIsTop) { if (parentIsTop) {
...@@ -1679,6 +1680,10 @@ class QueryGenerator { ...@@ -1679,6 +1680,10 @@ class QueryGenerator {
// Check for potential aliased JOIN condition // Check for potential aliased JOIN condition
joinOn = this._getAliasForField(tableName, attrLeft, topLevelInfo.options) || `${tableName}.${this.quoteIdentifier(attrLeft)}`; joinOn = this._getAliasForField(tableName, attrLeft, topLevelInfo.options) || `${tableName}.${this.quoteIdentifier(attrLeft)}`;
if (topLevelInfo.subQuery) {
subqueryAttributes.push(`${tableName}.${this.quoteIdentifier(fieldLeft)}`);
}
} else { } else {
const joinSource = `${asLeft.replace(/->/g, '.')}.${attrLeft}`; const joinSource = `${asLeft.replace(/->/g, '.')}.${attrLeft}`;
...@@ -1716,7 +1721,7 @@ class QueryGenerator { ...@@ -1716,7 +1721,7 @@ class QueryGenerator {
condition: joinOn, condition: joinOn,
attributes: { attributes: {
main: [], main: [],
subQuery: [] subQuery: subqueryAttributes
} }
}; };
} }
......
...@@ -2091,5 +2091,29 @@ describe(Support.getTestDialectTeaser('Include'), () => { ...@@ -2091,5 +2091,29 @@ describe(Support.getTestDialectTeaser('Include'), () => {
}); });
}); });
}); });
it('should be able to generate a correct request for entity with 1:n and m:1 associations and limit', function() {
return this.fixtureA().then(() => {
return this.models.Product.findAll({
attributes: ['title'],
include: [
{ model: this.models.User },
{ model: this.models.Price }
],
limit: 10
}).then( products => {
expect(products).to.be.an('array');
expect(products).to.be.lengthOf(10);
for (const product of products) {
expect(product.title).to.be.a('string');
// checking that internally added fields used to handle 'BelongsTo' associations are not leaked to result
expect(product.UserId).to.be.equal(undefined);
// checking that included models are on their places
expect(product.User).to.satisfy( User => User === null || User instanceof this.models.User );
expect(product.Prices).to.be.an('array');
}
});
});
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!