Skip to content

Cannot reset Order with Entity Ordering #9902

Closed as not planned
Closed as not planned

Description

@wodka

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.

Activity

  1. minhhieu76qng commented on Apr 29, 2024

    @minhhieu76qng

    Same my problem.
    Choosing this package to start new project is the my biggest mistake. Too much errors and unexpected behaviors.

  2. 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