Skip to content

OrderBy applied in modifiers can't be cleared #2747

@mariusbrn

Description

@mariusbrn

Objection.js version: 3.1.5
Node.js version: 20.11
Db engine: postgres

I have a User Model with a relation on events:

class User extends Model {

  static get relationMappings() {
    return {
      events: {
        relation: Model.HasManyRelation,
        modelClass: "Event",
        join: {
          from: "users.id",
          to: "events.userId",
        },
        modify(qb) {
          qb.orderBy("id", "desc");
        },
      },
    };
  }
}

when i try to get the total count of events for a user:

const query = user.$relatedQuery("events")

query.has("orderBy") // false

const totalCount = await query.resultSize()

Despite the this.clone().clear(/orderBy|offset|limit/);, the ORDER BY clause remains, resulting in a less performant query:

SELECT count(*) AS "count" FROM (
  SELECT "events".* FROM "events"
  WHERE "events"."user_id" IN (?)
  ORDER BY "id" DESC
) AS "temp";

Operations applied in modifiers do not seem to be recorded on the queryBuilder object. Is it intentional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions