Unable to find a valid association for model when ordering nested model | Sequelize

Viewed 20

I followed the official docs from sequelize but it doens't work ,I wanna order sessions by end_date , help me please.


GrouspSession hasMany Session

    const groupSessions = await groupSessionRepo.findAllGroupSessions({
      where: { teacher_id: userId },
      include: [
        {
          model: Class,
          attributes: {
            exclude: ['created_at', 'updated_at', 'deleted_at', 'id'],
          },
        },
        {
          model: Session,
          where: { end_date: { [Op.lte]: new Date() } },
          attributes: ['name', 'end_date', 'participants_nb'],
  
        
        },
      ],
order:[Session,"end_date","ASC"],
      attributes: [],
    });
1 Answers

Finally I found the solution, beacuse i am using sequelize-typescript so i faced a problem to define alias but i found that answer : here

then I solved like that:

    const groupSessions = await groupSessionRepo.findAllGroupSessions({
  where: { teacher_id: userId },
  include: [
    {
      model: Class,
      attributes: {
        exclude: ['created_at', 'updated_at', 'deleted_at', 'id'],
      },
    },
    {
      model: Session,
      where: { end_date: { [Op.lte]: new Date() } },
      attributes: ['name', 'end_date', 'participants_nb'],
      as: 'sessions',
    },
  ],
  order: [[{ model: Session, as: 'sessions' }, 'name', 'DESC']],
  attributes: [],
});
Related