排序问题按日期按升序排列查询



我需要按升序排序一个sequelize查询。我试图整理的是模型的结果:ExamScore(作为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']],
},
],
});

以下是此查询返回的示例:

{
"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"
}
]
},

我不明白我做错了什么。

尝试将订单添加到顶级查询中,而不是添加到includes中。

示例:

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']],
});

最新更新