You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Qualify columns with table alias in aggregate SELECT clauses for joined tables - #11817
What the change is intended to do:
This PR fixes an issue where repository aggregation methods sum, average, minimum and maximum throw "ambiguous column reference" errors when used with WHERE clauses that filter by relations.
Why this change is needed:
When using aggregation methods with relation filters (e.g., repository.maximum('id', { author: { id: 1 } }), TypeORM automatically creates LEFT JOINs to the related tables. However, the SELECT clause was generating unqualified column names like MAX("id") instead of MAX("TableAlias"."id"). This caused database errors when multiple tables in the join had columns with the same name (which is very common with id columns).
Pull-Request Checklist
Applied the fix to EntityManager.ts
Verified the fix resolves the issue with the reproduction case
The alias is set to metadata.name which is the entity name. This could potentially cause issues if the entity name contains special characters or conflicts with SQL reserved keywords. Consider using a sanitized or escaped alias name, or verify that metadata.name is always safe to use as a table alias.
The alias is escaped using this.connection.driver.escape() in the SELECT clause. Verify that this is the correct escaping method for table aliases (as opposed to column names or identifiers), and ensure it doesn't cause double-escaping issues if the alias was already processed elsewhere in the query builder chain.
Conditionally qualify the aggregate function's column with the table alias only when joins are present in the query. This avoids potential issues and generates cleaner SQL.
Why: The suggestion correctly identifies that the PR's change always qualifies the column with an alias. While this fixes the issue with joins, the suggestion's proposal to do so conditionally based on the presence of joins makes the generated SQL cleaner and more robust for cases without relations.
✅ Use the query builder's actual aliasSuggestion Impact:The commit directly implements the suggestion by creating the query builder first, calling setFindOptions, then retrieving the alias from the query builder instance (qb.alias) instead of using the hardcoded metadata.name value.
code diff:
- const alias = metadata.name- const result = await this.createQueryBuilder(entityClass, alias)- .setFindOptions({ where })++ const qb = this.createQueryBuilder(entityClass, metadata.name)+ qb.setFindOptions({ where })++ const alias = qb.alias++ const result = await qb
To prevent potential SQL errors, retrieve the alias from the query builder instance after setFindOptions is called, instead of using the hardcoded metadata.name.
-const alias = metadata.name-const result = await this.createQueryBuilder(entityClass, alias)- .setFindOptions({ where })+const qb = this.createQueryBuilder(entityClass, metadata.name)+qb.setFindOptions({ where })++const alias = qb.alias++const result = await qb
.select(
`${fnName}(${this.connection.driver.escape(
alias,
)}.${this.connection.driver.escape(column.databaseName)})`,
fnName,
)
.getRawOne()
[Suggestion processed]
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a potential bug where the hardcoded alias might differ from the one generated by the query builder after processing relations, leading to SQL errors. Applying this change makes the implementation more robust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of change
What the change is intended to do:
This PR fixes an issue where repository aggregation methods
sum,average,minimumandmaximumthrow"ambiguous column reference"errors when used with WHERE clauses that filter by relations.Why this change is needed:
When using aggregation methods with relation filters (e.g.,
repository.maximum('id', { author: { id: 1 } }), TypeORM automatically creates LEFT JOINs to the related tables. However, the SELECT clause was generating unqualified column names likeMAX("id")instead ofMAX("TableAlias"."id"). This caused database errors when multiple tables in the join had columns with the same name (which is very common withidcolumns).Pull-Request Checklist
EntityManager.tstest/github-issues/11797aggregate-methods-with-relationsmasterbranchFixes #00000tests/**.test.ts)docs/docs/**.md)Current behavior:
New behavior:
Fixes #11797