繁体   English   中英

Sequelize ORM 三表一对多join?

[英]Sequelize ORM Three Table One-to-Many Join?

我可能错误地用了我的帖子标题。 我有表 A,其 PK:“id”是表 B 中复合主的 FK。表 A 还保存表 C 中记录的复合 PK id 作为 FK。 我需要帮助编写一个 Sequelize 查询来查找与表 B 中的许多记录匹配的表 A ID,并且表 C FK 指向另一组要匹配的许多记录。

到目前为止,我有:

TableA.findAll({
  attributes: ["id"],
  where: {
    include: [
      {
        model: "TableB",
        where: {
          TableA_id: {$col: "TableA.id"}   // Need all records that share this      
        }
      },
      {
        model: "TableC",
        where: {
          TableC_value: [4, 5, 6], //I need the record where these all share the same key
          TableC_PK: {$col: TableA.TableC_FK} 
       }
      }
    ]
  }
})

我还没有测试过,但我也不知道我是否走在正确的轨道上。

Sequlize 不支持复合主键和外键,因此您需要在关联中指示一个字段并始终在include中使用on选项(请参阅我的回答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM