With Sequelize, I have two models with many to many association: Movie and Actor. I want to get all movies that movies that with to the current actors, but I don't understand how do.
get method:
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:
Considering below translates as follows:
User.findAll({include: [{model: Post, where: {active: true}}]
INNER JOIN `posts` AS `post` ON `users`.`id` = `post`.`user_id` AND `post`.`active`=true
I believe it's enough if you specify query for the table being joined:
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: [],
},
},
],
}),
Maybe you'll have to remove duplicates, not sure. Please notice that spaces in an URL:
http://localhost:3333/movie?actors=Keanu Reeves&actors=Ian McShane
should be encoded:
http://localhost:3333/movie?actors=Keanu%20Reeves&actors=Ian%20McShane
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.