【问题标题】:How to translate mysql query to sequelize orm如何将mysql查询翻译为sequelize orm
【发布时间】:2019-08-16 16:04:20
【问题描述】:

我需要使用 sequelize 执行这个多重 LEFT JOIN 查询:

SELECT movie, genre FROM `yt_movies` M 
LEFT JOIN `genres_link` GL ON M.id = GL.movie_id
LEFT JOIN `genres` G ON GL.genre_id = G.id
WHERE M.id = 1098

我试过了

const YtMovies = db.yt_movies;
const Genres = db.genres;
const GenresLink = db.genres_link;

YtMovies.hasMany(GenresLink, { as: 'GL', foreignKey: 'movie_id' });
YtMovies.hasMany(Genres, { as: 'G', foreignKey: 'genre' });

const res = await db.yt_movies.findAll({
                    attributes: ['movie'],
                    where: { id: movie_id },
                    include: [
                        {
                            model: db.genres_link,
                            as: 'GL',
                            required: false,
                            attributes: ['genre_id'],
                        },
                        {
                            model: db.genres,
                            required: false,
                            as: 'G',
                            attributes: ['genre'],
                        },
                    ],
                });

返回的查询看起来像

SELECT
`yt_movies`.`id`,
`yt_movies`.`movie`,
`GL`.`id` AS `GL.id`,
`GL`.`genre_id` AS `GL.genre_id`,
`G`.`id` AS `G.id`,
`G`.`genre` AS `G.genre`
FROM `yt_movies` AS `yt_movies`
LEFT OUTER JOIN `genres_link` AS `GL` ON `yt_movies`.`id` = `GL`.`movie_id`
LEFT OUTER JOIN `genres` AS `G` ON `yt_movies`.`id` = `G`.`genre`
WHERE `yt_movies`.`id` = 1098;

在最后两个字符串中,我们可以看到它使用 ON 和 yt_movies 但我希望它与genres_link一起使用ON

LEFT JOIN `genres` AS G ON GL.genre_id = G.id <-- expected

我想我不太了解表关联并且搞砸了 hasMany 或者我错过了另一个语句

表格看起来像

yt_movies

| id | movie   | pj | pq|
|----|---------|----|---|
|  1 |Avatar   |    |   |
|  2 |Predator |    |   |
|  3 |...      |    |   | 

genres_link

| id | genre_id| movie_id |
|----|---------|----------|
|  1 |  12     |    1     | // avatar
|  2 |  13     |    2     | // predator
|  3 |  14     |    2     | // predator

流派

| id | genre    |
|----|----------|
| 12 | action   | 
| 13 | thriller | 
| 14 | horror   | 

我所做的只是设法执行第一个 LEFT JOIN...添加第二个包含没有帮助,而且恐怕即使在阅读文档后我也不会真正理解表关联:|

我想我需要使用belongsToMany,但目前我不明白如何:))

感谢所有帮助! 谢谢你!

【问题讨论】:

  • 你能分享一下关系定义吗?
  • @RohitDalal 嘿,我附上了一些表格示例,我想我需要使用 belongsToMany,但目前我不明白如何:))
  • 不应该是 - 左外连接 genres AS G ON GL .genre_id = G.genre_id 你的 sequelize sql 不正确。 yt_movies 未与流派表链接。 Genres_link 与流派表链接
  • 请分享型号代码。可以通过 - pastebin.com 分享
  • @RohitDalal > 不应该 - 左外连接流派 AS G ON GL .genre_id = G.genre_id 你的续集 sql 不正确这就是我在说的。这是一个垃圾箱 - pastebin.com/dLdusCft

标签: javascript mysql left-join sequelize.js


【解决方案1】:

要加入 A->B->C,您应该将 C 的包含嵌套在 B 的包含中,例如

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

但是,如果表genres_link除了电影和流派的PK之外没有其他属性,则使用

   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 
          }
          ]
       });

manual 提供了一些关于此主题的有用信息...

【讨论】:

    猜你喜欢
    • 2018-11-06
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2019-09-21
    • 2023-03-23
    • 2022-12-29
    • 2015-10-08
    相关资源
    最近更新 更多