'Sequelize, sorting a query based on a literal

So I have this query, this will return multiple fields so what I did is I want to sum up a certain field and order it based on that new alias

 const salesOrders = await SalesOrders.findAndCountAll({
            attributes: ['grandTotal', 'refNo', 'userId', 'createdAt', 'deletedAt', [Sequelize.literal('SUM(grand_total)'), 'sumTotal']],
            limit: _.toInteger(limit || getLimitOffset(req).limit),
            offset: _.toInteger(offset || getLimitOffset(req).offset),
            separate: true,
            group: ['userId'],
            order: ['sumTotal', 'DESC'],
            where: {
              userId: {
                [Sequelize.Op.in]: userIds
              },
              ...transactionFilter
            },

However it's saying that it doesn't recognize "sumTotal", it's giving me this error:

 sqlMessage: "Unknown column 'SalesOrders.sumTotal' in 'order clause'",

Is there a way for sequelize to recognize my newly declared alias?



Sources

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

Source: Stack Overflow

Solution Source