按日期按升序排序 sequelize 查询的问题

Problem ordering sequelize query by date, in ascending order

我需要按日期升序对 sequelize 查询进行排序。我要整理的是模型的结果:ExamScore (as student_score).

请注意,在排序中我设置了“updated_at”列和排序方式。然而,它不起作用,它根本不订购。

const students = await Students.findAll({
    attributes: ['id', 'name', 'cpf'],
    include: [
      {
        model: Status,
        attributes: ['status', 'updated_at'],
        as: 'student_status',
        where: [{ course_id }],
        required: false,
      },
      {
        model: ExamScore,
        attributes: ['score', 'updated_at'],
        as: 'student_score',
        where: [{ module_id }],
        required: false,
        order: [['updated_at', 'ASC']],
      },
    ],
  });

下面是来自此查询的 return 的示例:

{
    "id": 30014,
    "name": "Jon Doe",
    "cpf": "xxxxxxxx",
    "student_status": [
        {
            "status": "APROVADO",
            "updated_at": "2021-05-27T03:06:14.502Z"
        }
    ],
    "student_score": [
        {
            "score": "9.00",
            "updated_at": "2021-05-27T03:06:13.998Z"
        },
        {
            "score": "2.00",
            "updated_at": "2021-05-27T02:59:22.571Z"
        }
    ]
},

我不明白我做错了什么。

尝试向您的热门查询添加顺序,而不是在包含中。

示例:

const students = await Students.findAll({
    attributes: ['id', 'name', 'cpf'],
    include: [
      {
        model: Status,
        attributes: ['status', 'updated_at'],
        as: 'student_status',
        where: [{ course_id }],
        required: false,
      },
      {
        model: ExamScore,
        attributes: ['score', 'updated_at'],
        as: 'student_score',
        where: [{ module_id }],
        required: false,
      },
    ],
    // add the order, model ExamScore with alias student_score
    order: [[{ model: ExamScore, as: 'student_score' }, 'updated_at', 'ASC']],
  });