Sequelize-SQL Server-关联表排序 [英] Sequelize - SQL Server - order by for association tables
问题描述
我有3个表,例如user
,userProfile
和userProfileImages
. User
与userPrfoile
进行映射,而userProfile
与userProfileImages
进行映射.
I have 3 tables such as user
, userProfile
and userProfileImages
. User
is mapping with userPrfoile
as has many and userProfile
is mapping with userProfileImages
as has many.
我需要在userProfileImages
中写出按查询顺序,我尝试了如下操作,但是没有运气.
I need to write the order by query in userProfileImages
, I tried as below, but no luck.
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
表的结果却不尽如人意.
I am getting the result, but userProfilePicture
table's result is not as desc.
请给出解决方案
推荐答案
来自Sequelize官方文档:
From Sequelize offical docs:
// 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'],
通过引用以上语法,如下更新您的订单选项
By referring above syntax, Update your order option like below
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/问题/4553
Refer this thread for more solutions: https://github.com/sequelize/sequelize/issues/4553
这篇关于Sequelize-SQL Server-关联表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!