我有以下关联:
M:N relationship table
ProductCategory.hasMany(Product, { foreignKey: 'productId' });
ProductCategory.hasMany(Category, { foreignKey: 'categoryId', required: true });
Category.belongsToMany(Product, {
through: {
model: ProductCategory,
},
otherKey: 'productId',
foreignKey: 'categoryId',
});
Product.belongsToMany(Category, {
through: {
model: ProductCategory,
unique: false,
},
otherKey: 'categoryId',
foreignKey: 'productId',
});
当我调用以下方法 CategoryModel.findAll({ include: [ {model: ProductModel, limit: 1} ] });
时,我得到如下信息:“只有 HasMany 关联支持 include.separate”
任何人都知道我该如何处理并限制包含(多对多)关系?
如果我删除限制,一切正常,我会得到相关产品的类别,但我想限制产品。
我将不胜感激任何帮助,问候。
最佳答案
Sequelize 仍然不支持对belongsToMany 关系的限制:
如本 questions 答案 1 中所述,针对您的情况的解决方法:
const categories = await CategoryModel.findAll();
const categoriedProductModels = await categories.getProductModels({limit:1});
替代问题是 1 , 2
关于node.js - 如何限制 Sequelize 上的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65803974/