gpt4 book ai didi

mysql - 如何限制Sequelize JS中belongsToMany关联的包含?

转载 作者:行者123 更新时间:2023-12-03 22:21:19 24 4
gpt4 key购买 nike

我目前在 Sequelize 中有以下模型:
Product

ProductList.belongsToMany(models.Product, { as: 'products', through: 
sequelize.models.ProductListProduct, foreignKey: 'productListId'});

和枢轴 ProductListProduct
我目前正在尝试让一系列 productList 显示在我的主页上,

并且需要将退回的产品限制为某个值(在这种情况下为 6):
let productLists = await ProductList.findAll({
where: {
slug: ['recommended', 'new_and_promos']
},
include: [{
model: Product,
as: 'products',
include: ['attributes', 'images'],
where: {
active: true
}
}],
order: [
[
'products', ProductListProduct, 'position', 'ASC'
]
]
})

这是当前的提取,但是如果我对包含添加一个限制,它会告诉我只有 hasMany 可以有 {separate: true}

回顾一下,我想要实现的是返回 n 个 ProductList,每个都只附加了 m 个 Product。

最佳答案

设法让它像这样工作,看起来并不理想,但它完成了工作:

let productLists = await ProductList.findAll({
where: {
slug: ['recommended', 'new_and_promos']
},
include: [{
model: Product,
as: 'products',
include: ['attributes', 'images'],
where: Sequelize.literal('`products->ProductListProduct`.`position` < 8')
}],
order: [
[
'products', ProductListProduct, 'position', 'ASC'
]
]
})

关于mysql - 如何限制Sequelize JS中belongsToMany关联的包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61726143/

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