'Sequelize sorting a query based on joined model column

Multiple times I find my self in a situation where I have a query like this, I have found solutions for these but it is pretty long and taxing on backend, I was wondering if it is possible in one query.

const payload = await ModelA.findAndCountAll({
   attributes: ['a', 'b', 'c']
   order: [ ['d','DESC'] ] 
   include:[
      model: ModelB
      attributes: ['d']
      as: 'modelB'
  ]
]})

the query above will produce an error below are the solutions that I've tried:

const payload = await ModelA.findAndCountAll({
   attributes: ['a', 'b', 'c']
   order: [ ['modelB.d','DESC'] ] 
   include:[
      model: ModelB
      attributes: ['d']
      as: 'modelB'
  ]
]})


const payload = await ModelA.findAndCountAll({
   attributes: ['a', 'b', 'c']
   order: [ [Sequelize.col('modelB.d'),'DESC'] ] 
   include:[
      model: ModelB
      attributes: ['d']
      as: 'modelB'
  ]
]})




const payload = await ModelA.findAndCountAll({
   attributes: ['a', 'b', 'c', [Sequelize.col('modelB.d'), 'subD']]
   order: [ 'subD','DESC'] ] 
   include:[
      model: ModelB
      attributes: ['d']
      as: 'modelB'
  ]
]})

All of them all doesn't work, is it possible to sort based on a joined model or do I have to resort to literal?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source