Repository navigation
Description
Issue Description
I would like to order the resultset by a column from a subquery which I have joined to the outer query. Unfortunately the typeorm library throws an error when using the query builder:
Exception TypeORMError: Cannot get entity metadata for the given alias "child"
Expected Behavior
To be able to execute a query to the DB using a query builder and order the results by a column from a sub query
Actual Behavior
Exception TypeORMError: Cannot get entity metadata for the given alias "child"
Steps to Reproduce
class Parent {
id: number
}
class Child {
parentId: number
initials: string
}
createQueryBuilder(Parent, 'parent').innerJoinAndSelect(subQuery=> {
return subquery.from(Child, 'child').select('parentId').addSelect('max(initials)', 'initials').groupBy(parentId)
}, 'child', 'parent.id = child.parentId').orderBy('child.initials')
The above code doesn't get as far as generating the SQL to the DB. The typeorm library fails for the 'orderBy' with the following error:
Exception TypeORMError: Cannot get entity metadata for the given alias "child"
f I remove the orderBy then the generated sql is similar to the following (typeorm should expand child.* to the appropriate columns - but it doesn't):
select ....., child.*
from parent parent
join (select parentId, max(initials) as initials from child) child
on child.parentId = parent.id
My Environment
| Dependency | Version |
| --- | --- |
| Operating System | |
| Node.js version | v18.8.0 |
| Typescript version | 4.8.3 |
| TypeORM version | 0.3.6 |
Additional Context
Relevant Database Driver(s)
| DB Type | Reproducible |
|-------------------| --- |
| aurora-mysql | no |
| aurora-postgres | yes |
| better-sqlite3 | no |
| cockroachdb | no |
| cordova | no |
| expo | no |
| mongodb | no |
| mysql | no |
| nativescript | no |
| oracle | no |
| postgres | no |
| react-native | no |
| sap | no |
| spanner | no |
| sqlite | no |
| sqlite-abstract | no |
| sqljs | no |
| sqlserver | no |
Are you willing to resolve this issue by submitting a Pull Request?
-
✅ Yes, I have the time, and I know how to start.
-
✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
-
✖️ No, I don’t have the time, but I can support (using donations) development.
-
✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
Your privacy and security are important to us. See our privacy policy (Canada, Europe & Asia, United States).
Activity
[-]Order by column from sunwueru[/-][+]Order by column from subqueru[/+][-]Order by column from subqueru[/-][+]Order by column from subquery[/+]yufengctbu commentedon Apr 24, 2023
I have encountered the same problem. Is there any solution for this problem?
eltongarbin commentedon May 17, 2023
Try downgrading to version 0.3.13. It worked for me
#4015 (comment)
Coder-Manuel commentedon Jul 28, 2023
Any solution for this ?
Having the same issue...
fix: resolve issue order subquery column
fix: resolve issue order subquery column
alumni commentedon Mar 19, 2025
One issue I see in the description is that usually sub-queries are generated differently:
github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.