繁体   English   中英

Sequelize - 如何在关联 n:n 中使用运算符 AND

[英]Sequelize - how to use operator AND in association n:n

使用 Sequelize,我有两个具有多对多关联的模型:电影和演员。 我想把所有与当前演员合作的电影都拿到,但我不明白怎么做。

获取方法:

http://localhost:3333/movie?actors=Keanu Reeves&actors=Ian McShane
Movie.findAll({
      where: { ...movieQuery },
      attributes: ['name', 'director', 'genre'],
      include: [
        {
          model: Actor,
          where: { ...actorsQuery },
          attributes: ['name'],
          through: {
            attributes: [],
          },
        },
      ],
    }),

结果:

结果

考虑以下翻译如下:

User.findAll({include: [{model: Post, where: {active: true}}]
INNER JOIN `posts` AS `post` ON `users`.`id` = `post`.`user_id` AND `post`.`active`=true

我相信如果您为要连接的表指定查询就足够了:

var actors = [{'name': 'Keanu Reeves'}, {'name': 'Ian McShane'}];
Movie.findAll({
  attributes: ['name', 'director', 'genre'],
  include: [
    {
      model: Actor,
      where: { [Op.or]: actors },
      attributes: ['name'],
      through: {
        attributes: [],
      },
    },
  ],
}),

也许您必须删除重复项,不确定。 请注意URL 中的空格:

http://localhost:3333/movie?actors=Keanu Reeves&actors=Ian McShane

应该编码:

http://localhost:3333/movie?actors=Keanu%20Reeves&actors=Ian%20McShane

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM