sequelize关联查询mysql包含?-灵析社区

今天吃什么你说吧

有两张表 `article` 与 `tag` 多对多关联 ArticleSchema.belongsToMany(CategorySchema, { through: { model: ArticleCategory, unique: false // 取消联合主键的约定 }, as: 'category', foreignKey: 'articleId', constraints: false }) CategorySchema.belongsToMany(ArticleSchema, { through: { model: ArticleCategory, unique: false }, as: 'article', foreignKey: 'categoryId', constraints: false }) 我现在需要通过`tag`来筛选关联的`article`应该怎么写呢 尝试了在 `where` 中查询,但是关联的属性是报错的,在 `include` 中来做筛选的话,将 `tag` 也进行了筛选 ![](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/e5f9e201f2237660bdce92f0ffa8e123.png) 想要得到的结果就是 `tag` 传入 `10` 或者 `11` 或者 `10和11` `tag` 都能返回图中的结果 有什么办法可以很简单的拿到呢

阅读量:18

点赞量:0

问AI
谁能阻止我删代码
const articles = await ArticleSchema.findAll({ include: [{ model: TagSchema, as: 'category', // 别名,根据你的模型定义来用 where: { id: [10, 11] }, // 你可以在这里指定一个或者多个标签ID through: { attributes: [], // 这会排除中间表的字段 } }] });