Sequelize – SQL Server – 按关联表sorting
我有3个表,如user
, userProfile
和userProfileImages
。 User
与userPrfoile
映射关系和userProfile
与userProfile
的映射关系一样多。
我需要在userProfileImages
通过查询来编写订单 ,我尝试了下面,但没有运气。
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。
请给解决scheme
来自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
请参阅此主题以获取更多解决scheme: https : //github.com/sequelize/sequelize/issues/4553