Node Sequelize 中预加载嵌套模型的排序结果 [英] Ordering results of eager-loaded nested models in Node Sequelize
问题描述
我有一组复杂的关联模型.这些模型使用连接表关联,每个连接表都有一个名为order"的属性.我需要能够查询父模型页面"并包含关联模型,并按字段顺序"对这些关联进行排序.
I have a complex set of associated models. The models are associated using join tables, each with an attribute called 'order'. I need to be able to query the parent model 'Page' and include the associated models, and sort those associations by the field 'order'.
以下内容对结果的排序顺序没有影响:
The following is having no effect on the results' sort order:
db.Page.findAll({
include: [{
model: db.Gallery,
order: ['order', 'DESC'],
include: [{
model: db.Artwork,
order: ['order', 'DESC']
}]
}],
})
推荐答案
相信你可以做到:
db.Page.findAll({
include: [{
model: db.Gallery
include: [{
model: db.Artwork
}]
}],
order: [
// sort by the 'order' column in Gallery model, in descending order.
[ db.Gallery, 'order', 'DESC' ],
// then sort by the 'order' column in the nested Artwork model in a descending order.
// you need to specify the nested model's parent first.
// in this case, the parent model is Gallery, and the nested model is Artwork
[ db.Gallery, db.ArtWork, 'order', 'DESC' ]
]
})
还有很多不同的方式,或者您在订购时可以做的事情.在此处阅读更多信息:https://sequelize.org/master/manual/model-querying-basics.html#ordering-and-grouping
There are also a bunch of different ways, or things you can do when ordering. Read more here: https://sequelize.org/master/manual/model-querying-basics.html#ordering-and-grouping
这篇关于Node Sequelize 中预加载嵌套模型的排序结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!