Skip to content

Commit

Permalink
fix(postgres): adds support for minifying through join aliases (#15897)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
JamieREvans and WikiRik committed May 1, 2023
1 parent f2a4535 commit a9fd501
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/dialects/abstract/query-generator.js
Expand Up @@ -1881,11 +1881,7 @@ https://github.com/sequelize/sequelize/discussions/15694`);
}
}

if (this.options.minifyAliases && asRight.length > 63) {
const alias = `%${topLevelInfo.options.includeAliases.size}`;

topLevelInfo.options.includeAliases.set(alias, asRight);
}
this.aliasAs(asRight, topLevelInfo);

return {
join: include.required ? 'INNER JOIN' : include.right && this._dialect.supports['RIGHT JOIN'] ? 'RIGHT OUTER JOIN' : 'LEFT OUTER JOIN',
Expand Down Expand Up @@ -2027,6 +2023,8 @@ https://github.com/sequelize/sequelize/discussions/15694`);
throughWhere = this.getWhereConditions(through.where, this.sequelize.literal(this.quoteIdentifier(throughAs)), through.model);
}

this.aliasAs(includeAs.internalAs, topLevelInfo);

// Generate a wrapped join so that the through table join can be dependent on the target join
joinBody = `( ${this.quoteTable(throughTable, throughAs)} INNER JOIN ${this.quoteTable(include.model.getTableName(), includeAs.internalAs)} ON ${targetJoinOn}`;
if (throughWhere) {
Expand Down Expand Up @@ -2054,6 +2052,18 @@ https://github.com/sequelize/sequelize/discussions/15694`);
};
}

/*
* Appends to the alias cache if the alias 64+ characters long and minifyAliases is true.
* This helps to avoid character limits in PostgreSQL.
*/
aliasAs(as, topLevelInfo) {
if (this.options.minifyAliases && as.length >= 64) {
const alias = `%${topLevelInfo.options.includeAliases.size}`;

topLevelInfo.options.includeAliases.set(alias, as);
}
}

/*
* Generates subQueryFilter - a select nested in the where clause of the subQuery.
* For a given include a query is generated that contains all the way from the subQuery
Expand Down

0 comments on commit a9fd501

Please sign in to comment.