objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

orderBy in modifiers doesn't work

Open michellechsy opened this issue 9 months ago • 2 comments

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

michellechsy avatar May 15 '24 06:05 michellechsy