【发布时间】:2021-01-20 06:11:14
【问题描述】:
我有以下关联:
M:N 关系表
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”
任何人都知道我该如何处理这个问题并限制包含(多对多)关系? 如果我删除所有预期的限制,我会获得包含相关产品的类别,但我想限制产品。
我会很感激任何帮助,问候。
【问题讨论】:
-
你能检查问题here是否满足你的要求吗?
-
CategoryModel.findAll({ include: [ {model: ProductModel} ] , limit: 1 }); 试试看这个链接查看详情https://sequelize.org/v5/manual/models-usage.html
标签: node.js sequelize.js relationship