Skip to content

fix: resolve issue order subquery column (Cannot get metadata of given alias) - #11343

Merged
gioboa merged 8 commits into
typeorm:masterfrom
trannhan0810:fix/order-by-subquery-column
Jan 16, 2026
Merged

gioboa merged 8 commits into
typeorm:masterfrom
trannhan0810:fix/order-by-subquery-column

Conversation

@trannhan0810

@trannhan0810 trannhan0810 commented Mar 16, 2025 •

Copy link
Copy Markdown
Contributor

Closes: #9420

Description of change

add condition to check if alias has metadata before access it to avoid the error 'Cannot get metadata for given alias'

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change "N/A"
  • The new commits follow conventions explained in [CONTRIBUTING.md]

@trannhan0810 trannhan0810 changed the title fix: resolve issue order subquery column fix: resolve issue order subquery column (Cannot get metadata of given alias) Mar 19, 2025
Comment thread test/github-issues/9420/issue-9420.ts Resolved
@OSA413

OSA413 commented Mar 19, 2025

Copy link
Copy Markdown
Collaborator

I would also include the test from the original issue to see if this actually fixes it.

@coveralls

coveralls commented Mar 19, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.937% (+0.001%) from 80.936%
when pulling 31297b0 on trannhan0810:fix/order-by-subquery-column
into 9792beb on typeorm:master.

@mguida22
mguida22 self-requested a review May 9, 2025 16:24
@mguida22 mguida22 linked an issue May 9, 2025 that may be closed by this pull request
@trannhan0810
trannhan0810 force-pushed the fix/order-by-subquery-column branch from 54a60da to 05ff7c4 Compare May 14, 2025 18:59
Comment thread src/query-builder/SelectQueryBuilder.ts Resolved
Comment thread test/github-issues/9420/issue-9420.ts Resolved
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2025 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@11343
    

commit: 31297b0

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR still valid?

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jan 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

(Review updated until commit 31297b0)

🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider


Action Required

1. Unprotected metadata access in pagination logic 🐞 Bug
Description
• The method createOrderByCombinedWithSelectExpression accesses alias.metadata without checking
  hasMetadata first (lines 3738-3740 and 3778-3780)
• When ordering by columns from subquery aliases during pagination, this will throw 'Cannot get
  entity metadata for the given alias' error
• The PR fixes the same issue in createOrderByExpression but leaves this pagination-specific
  method vulnerable to the identical problem

correctness

Code

src/query-builder/SelectQueryBuilder.ts[2572]

+                        if (alias?.hasMetadata) {
Evidence
The PR fixes createOrderByExpression by adding hasMetadata check, but
createOrderByCombinedWithSelectExpression has the same pattern without protection. This method is
called during pagination (line 3494) when skip/take is used with joins. The Alias class metadata
getter throws TypeORMError when _metadata is undefined. Subquery aliases created via .from() don't
have metadata.

src/query-builder/SelectQueryBuilder.ts[3738-3740]
src/query-builder/SelectQueryBuilder.ts[3778-3780]
src/query-builder/Alias.ts[41-48]
src/query-builder/QueryBuilder.ts[669-676]

Agent Prompt
## Issue Description
The method `createOrderByCombinedWithSelectExpression` accesses `alias.metadata` without checking if the alias has metadata first. This causes errors when ordering by columns from subquery aliases during pagination scenarios.

## Issue Context
This is the same issue that was fixed in `createOrderByExpression` at line 2572, but the fix was not applied to `createOrderByCombinedWithSelectExpression`. Subquery aliases (created via `.from('(subquery)', 'alias')`) don't have entity metadata, and accessing the metadata property throws a TypeORMError.

## Fix Focus Areas
- src/query-builder/SelectQueryBuilder.ts[3734-3752]
- src/query-builder/SelectQueryBuilder.ts[3774-3792]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider


Previous review results

Results up to commit 868f377


🐞 Bugs (0) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider

Great, No Issues Found!

Qodo reviewed your code and found no material issues that require review Grey Divider
Results up to commit d05de63


🐞 Bugs (1) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider
Remediation Recommended
1. Incomplete fix leaves similar vulnerabilities 🐞 Bug
Description
• The PR adds a hasMetadata check at line 2572 in createOrderByExpression, but identical
  vulnerability patterns exist in other methods
• Lines 3737 and 3777 in createOrderByCombinedWithSelectExpression access alias.metadata without
  checking hasMetadata
• These locations will throw the same 'Cannot get metadata of given alias' error when used with
  subquery aliases
• The fix is incomplete and leaves the codebase vulnerable to the same issue in different code paths

reliability

Code

src/query-builder/SelectQueryBuilder.ts[2572]

+                        if (alias?.hasMetadata) {
Evidence
The Alias class getter for metadata throws an error when _metadata is undefined. Subquery aliases
are created without metadata in createFromAlias. The same pattern of accessing alias.metadata exists
in createOrderByCombinedWithSelectExpression at lines 3737 and 3777, which will fail for subquery
aliases.

src/query-builder/Alias.ts[41-48]
src/query-builder/QueryBuilder.ts[669-676]
src/query-builder/SelectQueryBuilder.ts[3735-3737]
src/query-builder/SelectQueryBuilder.ts[3775-3777]

Agent Prompt
## Issue Description
The createOrderByCombinedWithSelectExpression method accesses alias.metadata without checking if the alias has metadata first. This causes the same 'Cannot get metadata of given alias' error that was fixed in createOrderByExpression when used with subquery aliases.

## Issue Context
Subquery aliases (created via .from("(SELECT ...)", "alias")) don't have entity metadata. The Alias class throws an error when accessing the metadata getter if _metadata is undefined. The fix in createOrderByExpression at line 2572 demonstrates the correct pattern: check alias?.hasMetadata before accessing alias.metadata.

## Fix Focus Areas
- src/query-builder/SelectQueryBuilder.ts[3730-3750]
- src/query-builder/SelectQueryBuilder.ts[3770-3790]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider
Qodo Logo

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit b98ac7d

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 868f377

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 31297b0

@gioboa
gioboa merged commit 77b6ca9 into typeorm:master Jan 16, 2026
38 checks passed
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Order by column from subquery Error: Cannot get entity metadata for the given alias

6 participants