Repository navigation
Closed as not planned
Labels
Description
Issue description
Adding a Entity Order cannot be unset by calling .orderBy()
Expected Behavior
I expect that when I call .orderBy() that the order would be reset.
/**
* Sets ORDER BY condition in the query builder.
* If you had previously ORDER BY expression defined,
* calling this function will override previously set ORDER BY conditions.
*
* Calling order by without order set will remove all previously set order bys.
*/
orderBy(): this;Actual Behavior
The order statement from the entity definition is still passed in.
Steps to reproduce
@Entity({
orderBy: {
idx: 'ASC',
},
})
export class SomeEntity {
@PrimaryGeneratedColumn()
public id: string
@Column({ default: 0 })
public idx: number = 0
}on this entity we now want to do a count like:
const result = someRepository.createQueryBuilder('s')
.select('COUNT(s.id)', 'count')
.orderBy()
.getRawOne<{ count?: number }>()this will create the query similar to:
SELECT COUNT("s"."id") as count FROM "some" "s" ORDER BY "s"."idx"there is a really ugly hackaround that fixes the problem:
const result = someRepository.createQueryBuilder('s')
.select('COUNT(s.id)', 'count')
.orderBy('RANDODM()')
.getRawOne<{ count?: number }>()
// SELECT COUNT("s"."id") as count FROM "some" "s" ORDER BY RANDOM()-> this will add an order statement that does not depend on the selected entries and therefor will work.
My Environment
| Dependency | Version |
|---|---|
| Operating System | Mac / Ubuntu / ... |
| Node.js version | 16, 18, 19 |
| Typescript version | 4.9 |
| TypeORM version | 0.3 |
Additional Context
based on the generated query I would not expect that the db driver would make a difference here
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don't know how to start. I would need guidance.
Reacted by DEEPAK, Hieu Do, Tolga Paksoy, Jens Tschirpig and Raphaël Bideau
Activity
minhhieu76qng commentedon Apr 29, 2024
Same my problem.
Choosing this package to start new project is the my biggest mistake. Too much errors and unexpected behaviors.
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.