Skip to content

Keep selections aliased as join columns in withGraphFetched() and relation queries (#111) - #340

Merged
lehni merged 1 commit into
nextfrom
fix/rc-aggregate-alias
Oct 12, 2026
Merged

lehni merged 1 commit into
nextfrom
fix/rc-aggregate-alias

Conversation

@lehni

@lehni lehni commented Oct 12, 2026 •

Copy link
Copy Markdown

What

withGraphFetched() and relation queries (RelationFindOperation) treat a selection without a column, of an aggregate, raw SQL or a subquery, as selecting the column that its alias names, like max('ownerId as ownerId'), raw('?? as ??', ['ownerId', 'ownerId']) or a subquery .as('ownerId'). They don't select that join column once more. The rule lives in one helper in Selection.js, used where these queries check for missing join columns. findSelection(), hasSelection() and hasSelectionAs() are unchanged, so withGraphJoined() keeps its strict id check: an aggregate, raw SQL or subquery selected under the name of an id column, like max('pets.id as id'), still throws.

Why

Aggregates are meant to select only their aliases since #111, and raw SQL and subqueries with an alias select only that alias too, but nothing matched these aliases. For a query like Pet.query().max('ownerId as ownerId').groupBy('ownerId').withGraphFetched('owner'), withGraphFetched() selected ownerId a second time as an internal selection and then removed it from the result, so the selected value was gone.

The alias only counts where objection reads the join columns from the result rows. In withGraphJoined(), such a selection under an id name would be used as the root model's id, so it stays an error there.

Tests

  • Integration: should keep an aggregate aliased as the join column (#111) fails on next.
  • Integration: should keep raw and subquery selections aliased as the join column (#111) fails with only aggregates matched.
  • Unit: should throw if the user selects raws, subqueries or aggregates under the names of the ids also checks max('pets.id as id').

Part of #53.

@lehni lehni added the 4.0 Breaking change, deferred to 4.0 (#53) label Oct 12, 2026
@lehni
lehni force-pushed the fix/rc-aggregate-alias branch from 5ac414a to 01c7b10 Compare October 12, 2026 20:03
@lehni lehni changed the title Match aggregates by their aliases in findSelection() (#111) Keep aggregates aliased as join columns in withGraphFetched() and relation queries (#111) Oct 12, 2026
@lehni
lehni force-pushed the fix/rc-aggregate-alias branch from 01c7b10 to 4d760a6 Compare October 12, 2026 20:59
@lehni lehni changed the title Keep aggregates aliased as join columns in withGraphFetched() and relation queries (#111) Keep selections aliased as join columns in withGraphFetched() and relation queries (#111) Oct 12, 2026
@lehni
lehni force-pushed the fix/rc-aggregate-alias branch from 4d760a6 to df9061b Compare October 12, 2026 21:02
@lehni
lehni force-pushed the fix/rc-aggregate-alias branch from df9061b to 0c03b67 Compare October 12, 2026 21:18
@lehni
lehni merged commit e732161 into next Oct 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 Breaking change, deferred to 4.0 (#53)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant