v1shva
v1shva

Reputation: 1599

How to order columns in nested models in sequelize?

I am trying to retrieve scheduled_class model in asc order using column startsAt. Associations for the models as follows.

I am first querying a database for a schedule_class then including a class associated to the model, then including all the scheduled classes associated to the class model where they fall within the given range. I was able to retrieve data without ordering them. When I add the order attribute to sequelize it gives following error. How can I retrieve scheduled classes in asc order respect to column startsAt? `

"message":"Unknown column 'class.name' in 'order clause'","code":500,"className":"general-error","data":{}

module.exports = function () {
      return function (context) {
        const sequelize = context.app.get('sequelizeClient');
        context.params.sequelize = {
          raw: false,
          include: [ {model: sequelize.models.attendance, paranoid: true} ],
          order: []
        };
        if(context.params.query && context.params.query.includeClasses) {
          let startDate = new Date();
          startDate.setMonth(startDate.getMonth() -1);
          startDate.setDate(1);
          let endDate = new Date();
          endDate.setMonth(endDate.getMonth() + 2);
          endDate.setDate(0);
          context.params.sequelize.include.push(
            {
              model: sequelize.models.classes,
              paranoid: true,
              include:[
                {
                  model:sequelize.models.teacher,
                  paranoid: true
                },
                {
                  model:sequelize.models.scheduled_class,
                  paranoid: true,
                  where: {
                    startsAt: {
                      $gte: startDate
                    },
                    endsAt: {
                      $lte: endDate
                    }
                  },
                },
                ],
              order: [[sequelize.models.teacher, 'id', 'DESC']]
            });
          context.params.sequelize.order.push([sequelize.models.classes, 'name', 'DESC']);
          //context.params.sequelize.order.push([sequelize.models.teacher, 'id', 'DESC']);
          //context.params.sequelize.order.push([sequelize.models.scheduled_class, 'startsAt', 'DESC']);
          delete context.params.query.includeClasses;
        }
      };
    };

Upvotes: 1

Views: 1123

Answers (1)

v1shva
v1shva

Reputation: 1599

I was able to resolve the issue by adding the parent model to the order clause.

module.exports = function () {
  return function (context) {
    const sequelize = context.app.get('sequelizeClient');
    context.params.sequelize = {
      raw: false,
      include: [ {model: sequelize.models.attendance, paranoid: true} ],
      order: []
    };
    if(context.params.query && context.params.query.includeClasses) {
      let startDate = new Date();
      startDate.setMonth(startDate.getMonth() -1);
      startDate.setDate(1);
      let endDate = new Date();
      endDate.setMonth(endDate.getMonth() + 2);
      endDate.setDate(0);
      context.params.sequelize.include.push(
        {
          model: sequelize.models.classes,
          paranoid: true,
          include:[
            {
              model:sequelize.models.teacher,
              paranoid: true
            },
            {
              model:sequelize.models.scheduled_class,
              paranoid: true,
              where: {
                startsAt: {
                  $gte: startDate
                },
                endsAt: {
                  $lte: endDate
                }
              },
            },
            ],
        });
      context.params.sequelize.order.push([sequelize.models.classes, sequelize.models.scheduled_class, 'startsAt', 'ASC']);
      delete context.params.query.includeClasses;
    }
  };
};

In here after including the class => scheduled_class model, I pushed the order by scheduled_class.startsAt ASC to order array.

Upvotes: 0

Related Questions