Repository navigation
Closed
Labels
Description
Issue Description
In some cases, the order of selected columns in the emitted SQL is different than the order than they were added to the SelectQueryBuilder with select and addSelect. This is a problem when using getQuery etc. to insert one sql query into another where the order matters, such as UNION ALL.
Expected Behavior
When I add selected columns to the SelectQueryBuilder with select and addSelect functions, the emitted SQL query should preserve the order.
Actual Behavior
The emitted SQL reorders the columns.
Steps to Reproduce
- Create the entity
import { Column, Entity, PrimaryColumn } from "typeorm";
@Entity({ database: "Risk", name: "RMEconomicExposures" })
export class RMEconomicExposure {
@PrimaryColumn({ name: "ManagerId", type: "int" })
investmentId: number;
@PrimaryColumn({ name: "ReportDate", type: "datetime" })
reportDate: Date;
@PrimaryColumn({ name: "TierLevel", type: "varchar" })
tierLevel: string;
@Column({ name: "Measure", type: "float" })
measure: number;
@Column({ name: "AppCode", type: "int", nullable: true })
appCode: number | null;
@Column({ name: "CreateDate", type: "datetime", nullable: true })
createDate: Date | null;
@Column({ name: "CreateUser", type: "varchar", nullable: true })
createUser: string | null;
@Column({ name: "UpdateDate", type: "datetime", nullable: true })
modifyDate: Date | null;
@Column({ name: "UpdateUser", type: "varchar", nullable: true })
modifyUser: string | null;
}- Write the query with typeorm.
await getConnection("Foo").transaction(async manager => {
console.log(manager
.createQueryBuilder()
.subQuery()
.from(RMEconomicExposure, "m")
.where(`m.investmentId in (${investmentIds})`)
.select("m.investmentId", "investmentId")
.addSelect("m.reportDate", "reportDate")
.addSelect("m.tierLevel", "tierLevel")
.addSelect("''", "categoryLevel")
.addSelect("cast(null as float)", "rcg")
.addSelect("cast(null as float)", "rcgEst")
.addSelect("m.measure", "rm")
.getQuery());
})- The output reorders the columns:
(SELECT "m"."ManagerId" AS "investmentId", "m"."ReportDate" AS "reportDate", "m"."TierLevel" AS "tierLevel",
"m"."Measure" AS "rm", '' AS "categoryLevel", cast(null as float) AS "rcg", cast(null as float) AS "rcgEst"
FROM "Risk".."RMEconomicExposures" "m" WHERE "m"."ManagerId" in (...))
Notice the column "rm" was moved from last place to between "tierLevel" and "categoryLevel".
My Environment
| Dependency | Version |
|---|---|
| Operating System | MacOS Monterrey 12.0.1 |
| Node.js version | 16.13.0 |
| Typescript version | 4.5.2 |
| TypeORM version | 0.2.41 |
Additional Context
Relevant Database Driver(s)
| DB Type | Reproducible |
|---|---|
aurora-data-api |
no |
aurora-data-api-pg |
no |
better-sqlite3 |
no |
cockroachdb |
no |
cordova |
no |
expo |
no |
mongodb |
no |
mysql |
no |
nativescript |
no |
oracle |
no |
postgres |
no |
react-native |
no |
sap |
no |
sqlite |
no |
sqlite-abstract |
no |
sqljs |
no |
sqlserver |
yes |
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.
Reacted by Sato Naoya, Rayyan Ul Haq, Michał Wadas, Vicary, Ellie Corvinova, Christian A. Rodriguez, Chris Foster, koxu1996, Fernando Buccella, Darko Janković and 1 more
Activity
mrfoster commentedon Mar 17, 2023
It looks like
queryBuilder.expressionMap.selectsis still in the correct order. The following hack may help to correct the resultant key order until a fix is available.pcnova commentedon Nov 9, 2023
I ran into this and realized that if everything is double-quoted properly when handing it to TypeORM, then it won't reorder the selects. That is, instead of this:
...do this:
It seems like this way, TypeORM sees that it doesn't need to try and fix quotes, so it doesn't touch the select statement when you call
getQuery().Note 1: In my case, I am even quoting things that technically don't need it, like
"m"."id"- not sure whether this is needed to avoid the issue.Note 2: The
'investmentId'alias in the example above does not need double quotes - this is because TypeORM insists on adding double-quotes to this itself, so if you add them, you'll end up with invalid SQL. 😬dberardo-com commentedon Jun 25, 2025
i am not sure if it is related, but i would like to override the value of a virtual column using addSelect in the querybuilder, but it seems like virtual columns get added after the query builder select statements.
dberardo-com commentedon Feb 2, 2026
@gioboa do you believe my comment from above is related ?
gioboa commentedon Feb 2, 2026
Thanks @mrfoster Do you have a specific issue with this change?