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 in modifiers doesn't work #2656

Open
michellechsy opened this issue May 15, 2024 · 2 comments
Open

orderBy in modifiers doesn't work #2656

michellechsy opened this issue May 15, 2024 · 2 comments

Comments

@michellechsy
Copy link

Hi Team,

when i define the modifiers in my Model and then query it as a relation in another Model, the orderBy is not generated in the final query (with knex.js) while where condition works. I also check the samples provided in your code, i'm writing the same but with 'desc' order! Can you please help ?

// in my relation Model A:
  static get modifiers() {
    return {
      lastSession (builder) {
        builder.where('status', '<>', 'CURRENT')
      },
      orderByTime (builder) {
        builder.orderBy('createdAt', 'desc')
      }
    }
  }

in my Model B, i define a HasMany relation with Model A.

static get relationMappings() {
    return {
      sessions: {
        relation: Model.HasManyRelation,
        modelClass: ModelA,
        join: {
          from: 'ModelB.id',
          to: 'ModelA.userId'
        }
      }
}

then i query Model B with model A:

ModelB.query().orderBy('id', 'asc').withGraphFetched('sessions(lastSession, orderByTime)')

the result of sessions will always return by id (asc), instead of 'createdAt desc'.

@kapouer
Copy link
Contributor

kapouer commented Aug 30, 2024

Can you check what happens when you declare the modifiers like this:

ModelB.query().orderBy('id', 'asc').withGraphFetched('sessions(lastSession, orderByTime)').modifiers({
      lastSession (builder) {
        builder.where('status', '<>', 'CURRENT')
      },
      orderByTime (builder) {
        builder.orderBy('createdAt', 'desc')
      }
})

@epam-vendor
Copy link

epam-vendor commented Sep 5, 2024

Hi @kapouer ,
I've changed my code logic to avoid that modifier, but now i got another issue:
ModelA.query().leftJoinRelated('modelB').select(modelA.*, modelB.createdAt as bCreatedAt).orderBy('modelB.createdAt', 'desc')
when i use this kindof query with pagination (objection-cursor), in my second page call, it passed the modelA.createdAt value to my pagination instead of modelB.createdAt based on the knex debug query ( where ([modelB].[created_at] < ?)),
how should i fix it

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

3 participants