gpt4 book ai didi

node.js - Node Sequelize 中预加载嵌套模型的排序结果

转载 作者:IT老高 更新时间:2023-10-28 21:54:18 26 4
gpt4 key购买 nike

我有一组复杂的关联模型。这些模型使用连接表关联,每个连接表都有一个名为“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

关于node.js - Node Sequelize 中预加载嵌套模型的排序结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com