【问题标题】:Sequelize ORM Three Table One-to-Many Join?Sequelize ORM 三表一对多连接?
【发布时间】:2022-01-18 08:04:49
【问题描述】:

我可能错误地用了我的帖子标题。 我有表 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} 
       }
      }
    ]
  }
})

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

【问题讨论】:

    标签: mysql node.js join sequelize.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 2016-09-27
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 2015-05-28
      相关资源
      最近更新 更多