objection.js
objection.js copied to clipboard
orderBy in modifiers doesn't work
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'.