…erences
When using repository aggregation methods (sum, average, minimum, maximum)
with WHERE clauses that include relations, TypeORM creates LEFT JOINs to
related tables. Previously, the SELECT clause used unqualified column names
like MAX("id"), which caused "ambiguous column reference" errors when multiple
tables had columns with the same name.
This fix qualifies the column name with the table alias in the SELECT clause,
generating SQL like MAX("TableName"."id") instead of MAX("id").
Closes typeorm#11797
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
Note
This PR replaces #11817
History was rewritten, so GitHub auto-closed the previous PR.