且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

续集多对多查询问题

更新时间:2021-10-22 08:48:15

我认为你应该使用 belongsToMany 关联在这里.

I think you should use belongsToMany association here.

你可以这样定义关联

Product.belongsToMany(Category, { through: ProductCategory, foreignKey: 'product_id' });
Category.belongsToMany(Product, { through: ProductCategory, foreignKey: 'category_id' });

查询可以是

Product.findAll({
  include: [Category]
}).then((res) => {
  console.log(res);
})