Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderBy applied in modifiers can't be cleared #2747

Open
mariusbrn opened this issue Feb 26, 2025 · 0 comments
Open

OrderBy applied in modifiers can't be cleared #2747

mariusbrn opened this issue Feb 26, 2025 · 0 comments

Comments

@mariusbrn
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant