2017-11-26 7 views
0

私はReceiptItemsの関連する行、互いにhasManyメソッドとbelongsToメソッドに接続されているReceiptsからの支払いを取得しようとしています。Sequelize.Js-PostgreSQL関連するテーブルの行を所属テーブルから取得する方法は?

ここに私のテーブルのリレーション:ここ

// 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と支払いを取得しません領収書(ヌル変数ではなく、正常なカムバックの結果、関連するテーブルに関連するフィールドは表示されません)

答えて

0

私はこの問題に遭遇した理由を知りました。

まず、私は余分な終了ブロック括弧を持っていたし、私は以下の方法で関連付けを使用する必要がちょうどどこ文の後:

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

関連する問題