使用 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/