我有 3 个表,例如 user
、userProfile
和 userProfileImages
。 User
与 userPrfoile
有很多映射,userProfile
与 userProfileImages
有很多映射。
我需要在 userProfileImages
中编写order by query,我试过如下,但没有成功。
User.findById(uID, {
include: [
model: sequelize.models.userProfile
as: userProfile,
include: [
{
model: sequelize.models.userProfileImages,
as: 'profileImages',
}
],
order: [[sequelize.models.userProfileImages.id, "desc"]]
// order: [["id", "desc"]] --> Tried this way also no luck
] }
我得到了结果,但是 userProfilePicture
表的结果不是 desc。
请给出解决方案
最佳答案
来自 Sequelize 官方文档:
// Will order by an associated model's created_at using an association object. (preferred method)
[Subtask.associations.Task, 'createdAt', 'DESC'],
// Will order by a nested associated model's created_at using association objects. (preferred method)
[Subtask.associations.Task, Task.associations.Project, 'createdAt', 'DESC'],
通过引用上面的语法,如下更新您的订单选项
User.findById(uID, {
include: [{
model: sequelize.models.userProfile
as: userProfile,
include: [{
model: sequelize.models.userProfileImages,
as: 'profileImages',
}],
order: [['profileImages','id', 'desc']]
}]
});
官方文档:http://docs.sequelizejs.com/manual/tutorial/querying.html#ordering
有关更多解决方案,请参阅此线程:https://github.com/sequelize/sequelize/issues/4553
关于node.js - Sequelize - SQL Server - 关联表的排序依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027310/