问题描述
我目前在 Sequelize 中有以下模型:
I currently have the following models in Sequelize:
产品
和
ProductList.belongsToMany(models.Product, { as: 'products', through:
sequelize.models.ProductListProduct, foreignKey: 'productListId'});
和枢轴,ProductListProduct
我目前正在尝试让一系列 productList
显示在我的主页上,
I am currently trying to get a series of productList
to show on my homepage,
并且需要将退回的产品限制为特定值(在本例中为 6):
and would need to limit the returned products to a certain value (6 in this case):
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}
;
This is the current fetch, however if I add a limit to the include, it tells me that only hasMany can have {separate: true}
;
总结一下,我想要实现的是返回 n 个 ProductList,每个 ProductList 只附加了 m 个 Product.
To recap, what I'm trying to achieve is to return n ProductList, each with just m Product attached.
推荐答案
设法让它像这样工作,看起来并不理想,但它完成了工作:
Managed to get it working like this, doesn't look ideal but it does the job:
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'
]
]
})
这篇关于如何限制Sequelize JS中belongsToMany关联的包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!