Skip to content Skip to sidebar Skip to footer

How To Translate Mysql Query To Sequelize Orm

I need to perform this multiple LEFT JOIN query with sequelize: SELECT movie, genre FROM `yt_movies` M LEFT JOIN `genres_link` GL ON M.id = GL.movie_id LEFT JOIN `genres` G ON GL.

Solution 1:

To join A->B->C, you should nest the include for C inside the include for B, e.g.

A.findAll({    
    include: [
       {
         model: B,
         include: [
             {model: C}
             ]
         }
     ]
     })

But, if table genres_link has no attributes other than PKs of movie and genre, then use through.

YtMovies.belongsToMany(Genres, {through:GenresLink, foreignKey:'movie_id' });Genres.belongsToMany(YtMovies,{through:GenresLink,foreignKey:'genre_id '});YtMovies.findAll({include: [
          {
           model:Genres, 
           required :true,
           through:GenresLink 
          }
          ]
       });

The manual has some helpful info on this topic...

Post a Comment for "How To Translate Mysql Query To Sequelize Orm"