Skip to content

Broken maximum\minimum queries when default orderBy is defined #11213

Closed

Description

@healroot

Issue description

When you define orderBy for entity, its also added to min/max queries which leads to "QueryFailedError"

Expected Behavior

Min/Max queries should not contain "ORDER BY" clause even if entity define the default one

Actual Behavior

Default "ORDER BY" is added to Min/Max queries which leads to "QueryFailedError"
Query example

SELECT MAX("cost") AS "MAX" FROM "offer" "Offer" ORDER BY "Offer"."cost" DESC

Steps to reproduce

import {
    Column,
    Entity,
    Index,
    PrimaryGeneratedColumn,
} from "typeorm";
    
@Entity({ orderBy: { cost: "DESC" } })
export default class Offer {
    @PrimaryGeneratedColumn("uuid")
    declare id: string;

    @Index()
    @Column("float", { nullable: false, default: 0 })
    declare cost: number;
}

// Run this somewhere in your code
await repo.maximum("cost")

My Environment

Dependency Version
Operating System Ubuntu 24.04.1 LTS
Node.js version v20.12.2
Typescript version 5.4.3
TypeORM version 0.3.20

Additional Context

No response

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. healroot commented on Dec 30, 2024

    @healroot
    Author

    The base idea for fix was to remove ordering inside callAggregateFun, but it leads me to another bug #9902

  2. healroot commented on Dec 31, 2024

    @healroot
    Author

    Solution I came to is to add .setOption("disable-global-order") inside callAggregateFun from EntityManager

    What we have today:

    async callAggregateFun(entityClass, fnName, columnName, where = {}) {
            const metadata = this.connection.getMetadata(entityClass);
            const column = metadata.columns.find((item) => item.propertyPath === columnName);
            if (!column) {
                throw new error_1.TypeORMError(`Column "${columnName}" was not found in table "${metadata.name}"`);
            }
            const result = await this.createQueryBuilder(entityClass, metadata.name)
                .setFindOptions({ where })
                .select(`${fnName}(${this.connection.driver.escape(column.databaseName)})`, fnName)
                .getRawOne();
            return result[fnName] === null ? null : parseFloat(result[fnName]);
        }

    What I suggest

    async callAggregateFun(entityClass, fnName, columnName, where = {}) {
            const metadata = this.connection.getMetadata(entityClass);
            const column = metadata.columns.find((item) => item.propertyPath === columnName);
            if (!column) {
                throw new error_1.TypeORMError(`Column "${columnName}" was not found in table "${metadata.name}"`);
            }
            const result = await this.createQueryBuilder(entityClass, metadata.name)
                .setFindOptions({ where })
                .setOption("disable-global-order")
                .select(`${fnName}(${this.connection.driver.escape(column.databaseName)})`, fnName)
                .getRawOne();
            return result[fnName] === null ? null : parseFloat(result[fnName]);
        }
  3. github-actions commented on Sep 17, 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.

  4. healroot commented on Sep 18, 2025

    @healroot
    Author

    It should be an easy fix, which provides value
    So I believe its better to keep it in the backlog

  5. reopened this on Sep 18, 2025
  6. gioboa commented on Sep 18, 2025

    @gioboa
    Collaborator

    Thanks for the heads up @KharchenkoDmitriy
    Would you like to try to fix it with a PR?

  7. added this to the 1.0 milestone on Mar 9, 2026
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions