Sequelize.Js-PostgreSQL 如何从属于 table 的关联 table 中获取行?

Sequelize.Js-PostgreSQL How to get rows in associated tables from belonging table?

我正在尝试实现检索 ReceiptItems 中的关联行,从彼此连接的收据付款 hasMany 和 belongsTo 方法。

这是我的 table 关系:

// One to Many Relationship between receiptitems and receipts
db.Receipts.hasMany(db.ReceiptItems,{ foreignKey : 'receipt_id'});
db.ReceiptItems.belongsTo(db.Receipts,{ foreignKey : 'receipt_id'});

// One to Many Relationship between ReceiptItems and Payments
// This relation exists due to solve the problem of paying the debts later on !
db.Receipts.hasMany(db.Payments, { foreignKey : 'receipt_id' });
db.Payments.belongsTo(db.Receipts, { foreignKey : 'receipt_id' });

// One to many Relationship between Receipts and Plates
db.Plates.hasMany(db.Receipts, { foreignKey : 'plate_id' });
db.Receipts.belongsTo(db.Plates, { foreignKey : 'plate_id' });

这是我查找属于 plate_id 的收据的方法,它找到了收据,但它没有检索与该收据关联的 ReceiptItems 和付款(不是空变量,我看不到与相关联的 table 是成功复出的结果。)

db.Receipts.findAll({
        where : {
          plate_id : result.plate_id
        }
      },{
        include : [ { associaton : db.ReceiptItems}, { association : db.Payments} ]
      }).then((receiptResult)=>{
        console.log(receiptResult);
      }).catch((receiptErr)=>{
        console.log(receiptErr);
      })

我刚刚找到问题的原因。

首先,就在 where 语句之后,我有一个额外的结束块括号,我必须按以下方式使用关联:

db.Receipts.find({
        where : {
          plate_id : result.plate_id
        },
        include : [{ association : db.Receipts.associations.ReceiptItems},{ association : db.Receipts.associations.Payments }]
      }).then((receiptResult)=>{
        console.log(receiptResult);
      }).catch((receiptErr)=>{
        console.log(receiptErr);
      })