Skip to content

fix: keep join aliases within the database identifier limit - #1174

Open
RClaudiuM wants to merge 2 commits into
ppetzold:masterfrom
RClaudiuM:fix/1172-long-join-aliases
Open

RClaudiuM wants to merge 2 commits into
ppetzold:masterfrom
RClaudiuM:fix/1172-long-join-aliases

Conversation

@RClaudiuM

@RClaudiuM RClaudiuM commented Nov 7, 2026 •

Copy link
Copy Markdown

Fixes #1172

What

Relation joins get path-derived aliases (__root_<relation>_rel_<nested>_rel, and _rel_<name>_<depth>_<path> inside the EXISTS subqueries for relation filters). Long relation names push these past the 63-character identifier limit of Postgres. Postgres silently truncates them, so:

  • two joins sharing a long prefix get the same alias: table name "__root_relationWithAnExceptionallyLongNameThatOverflowsTheAlias" specified more than once
  • relation filters fail with missing FROM-clause entry for table "_rel_targetsReachedThroughAVeryLongRelationName_0_targetsReache"

How

A new getJoinAlias(qb, alias) helper returns the alias unchanged when it fits the driver's maxAliasLength (63 on Postgres and MySQL/MariaDB, no limit on SQLite). When the alias is too long, it cuts it and appends an 8-character hash of the full alias, so the result is deterministic and distinct. Every place that creates or references a join alias uses the helper on the full path-derived alias:

  • fixColumnAlias
  • addRelationsFromSchema
  • the joins built for a nested config where
  • polymorphic (~) filter joins
  • the EXISTS subquery aliases

That way, each reference to a given path resolves to the same alias. Short aliases don't change, so existing queries are unaffected.

Tests

src/__tests__/long-alias.entity.ts adds entities whose relation names produce aliases over 63 characters. They are registered with the paginate.spec.ts data source, and a new join aliases longer than the database identifier limit block there covers loading, sorting, filtering and searching two such relations (plus a nested one), to-many/EXISTS filters, a nested config where, and a polymorphic filter. src/alias-length.spec.ts has unit tests for getJoinAlias. (The database tests started out in their own spec file with their own data source, but dropSchema there raced paginate.spec.ts on the shared MariaDB database in CI, so they now share its data source.)

  • Before the fix, with DB=postgres, all 4 query tests fail with the errors above.
  • After the fix, all pass on Postgres and SQLite.
npm run format:ci && npm run lint && npm run build
DB=postgres npm run test:cov   # 380 passed
DB=sqlite npm run test:cov     # 353 passed

I didn't run the MariaDB suite locally. The test entities use short join column names so their tables fit MariaDB's 64-character column limit.

Relation joins use path-derived aliases such as `__root_<relation>_rel_<nested>_rel`. With long
relation names these exceed the 63 character identifier limit of Postgres, which silently
truncates them, so two joins sharing a long prefix end up with the same alias ("table name ...
specified more than once"), and the long aliases of the EXISTS subqueries for relation filters
can no longer be resolved.

Aliases longer than the driver's maxAliasLength are now cut and suffixed with a short hash of
the full alias. The shortening is applied wherever a join alias is created or referenced
(relations, config where, polymorphic filters, relation filter subqueries and fixColumnAlias),
so every reference to the same path resolves to the same alias. Aliases within the limit are
unchanged.

Fixes ppetzold#1172
alias-length.spec.ts opened its own data source on the same database with
dropSchema and synchronize. Jest runs spec files in parallel, so on MariaDB
it dropped the cat tables while paginate.spec.ts was running ("Table
'test.cat_friends_cat' doesn't exist").

Register the long alias entities with the paginate.spec.ts data source and
move the database tests there. alias-length.spec.ts keeps the getJoinAlias
unit tests, which need no database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres relation alias gets cut off at 63 characters, creating duplicate alias names

1 participant