我正在尝试使用列startsAt以asc顺序检索scheduled_class模型。 模型的关联如下。
我首先在数据库中查询schedule_class,然后包含与模型关联的类,然后包括与类模型关联的所有调度类,它们位于给定范围内。我能够在不订购数据的情况下检索数据。当我将order属性添加到sequelize时,它会产生以下错误。如何在asc命令中检索预定的类,以启动列启动? `
"消息":"未知列' class.name'在' order子句'","代码":500," className":" general-error","数据":{}
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;
}
};
};
答案 0 :(得分:0)
我能够通过将父模型添加到订单子句来解决问题。
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;
}
};
};
在此之后包括class => scheduled_class模型,我通过scheduled_class.startsAt ASC将订单推送到订单数组。