Skip to content

Order by column from subquery #9420

Closed

Description

@stuartf123

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

  1. changed the title [-]Order by column from sunwueru[/-] [+]Order by column from subqueru[/+] on Sep 30, 2022
  2. changed the title [-]Order by column from subqueru[/-] [+]Order by column from subquery[/+] on Sep 30, 2022
  3. yufengctbu commented on Apr 24, 2023

    @yufengctbu

    I have encountered the same problem. Is there any solution for this problem?

  4. eltongarbin commented on May 17, 2023

    @eltongarbin

    I have encountered the same problem. Is there any solution for this problem?

    Try downgrading to version 0.3.13. It worked for me

    #4015 (comment)

  5. Coder-Manuel commented on Jul 28, 2023

    @Coder-Manuel

    Any solution for this ?
    Having the same issue...

  6. added 2 commits that reference this issue on Mar 16, 2025
    3a92adf
    05ff7c4
  7. alumni commented on Mar 19, 2025

    @alumni
    Collaborator

    One issue I see in the description is that usually sub-queries are generated differently:

    qb => qb.subQuery().from()...
  8. github-actions commented on Sep 18, 2025

    @github-actions

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions