我目前在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'
]
]
})
这是当前的获取,但是如果我对include添加一个限制,它告诉我只有hasMany可以拥有{separate: true}
;
总而言之,我要实现的是返回n个ProductList,每个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'
]
]
})