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
中编写 order by query,我尝试如下,但没有运气.
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
表的结果与 desc 不一样.
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/issues/4553
Refer this thread for more solutions: https://github.com/sequelize/sequelize/issues/4553
这篇关于Sequelize - SQL Server - 关联表的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!