admin 管理员组文章数量: 1184232
Sequelize
我有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 表的结果不像描述的那样。
请给出解决方案
回答如下:来自 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/manual/tutorial/querying.html#ordering
有关更多解决方案,请参考此线程:https://github/sequelize/sequelize/issues/4553
本文标签: Sequelize
版权声明:本文标题:Sequelize 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1717046430a700239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论