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

标签 javascript node.js sequelize.js

使用 Sequelize,我有两个具有多对多关联的模型:电影和 Actor 。
我想获得所有与当前 Actor 合作的电影,但我不明白该怎么做。
获取方法:

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: [],
          },
        },
      ],
    }),
结果:
result

最佳答案

考虑如下翻译如下:

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: [],
      },
    },
  ],
}),
也许您必须删除重复项,不确定。 请注意 网址中的空格:
http://localhost:3333/movie?actors=Keanu Reeves&actors=Ian McShane
应该编码:
http://localhost:3333/movie?actors=Keanu%20Reeves&actors=Ian%20McShane

关于javascript - Sequelize - 如何在关联中使用运算符 AND n :n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63419293/

相关文章:

javascript - 如何将项目的最新版本推送到存储库?

javascript - 将 Javascript onClick 事件添加到 HTML 复选框阻止了我 'unchecking' 复选框

javascript - FilesizeWatcherSpec 上的 jasmine-node 没有输出 - 新手警报

node.js - 如何在 Sequelize.js 中使用数据库连接池

javascript - 为什么我的 Sequelize 查询对小数列求和并以字符串形式返回

javascript - 多功能合而为一

javascript - 使用 Javascript 的联系表

node.js - 将参数传递给 npm run script 命令

node.js - 如何在Web上使用Node js和socket io从BW08车辆跟踪器(GPS)获取数据?

javascript - 未处理的 Promise sequelize hooks 和加密密码