gpt4 book ai didi

javascript - 如何在Sequelize中按多对多关系排序?

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:25 24 4
gpt4 key购买 nike

我通过UserCategory用户类别之间建立了多对多关系,如下所示。

let user = await User.findAll({
where: {
id: req.query.user
},
attributes: ["id", "name"],
include: [
{
model: models.Category,
as: "interests",
attributes: ["id", "name", "nameTH", "icon"],
through: {
model: models.UserCategory,
as: "user_categories",
attributes: ["id", "userId", "categoryId", "updatedAt"]
}
}
],
// Here, I want to order by updatedAt in user_categories
order: [["user_categories", "updatedAt", "DESC"]]
});

How can I order the result by "updatedAt" inside UserCategory model?

最佳答案

请引用以下代码在UserCategory模型中按updatedAt对结果进行排序-

let user = await User.findAll({
where: {
id: req.query.user
},
attributes: ["id", "name"],
include: [
{
model: models.Category,
as: "interests",
attributes: ["id", "name", "nameTH", "icon"],
through: {
model: models.UserCategory,
as: "user_categories",
attributes: ["id", "userId", "categoryId", "updatedAt"]
}
}
],
// Here, I want to order by updatedAt in user_categories
order: [[Sequelize.literal('`interests->user_categories`.`updatedAt`'), 'DESC']]
});

希望对您有帮助!

关于javascript - 如何在Sequelize中按多对多关系排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60406603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com