2017-11-26 1 views
0

나는 hasMany 및 belongsTo 메소드에 서로 연결된 ReceiptItems, Payments에서 연관된 행을 검색하려고합니다.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 및 결제를 검색하지 않습니다 영수증 (null이 아님, 성공적인 컴백으로 인해 관련된 테이블과 관련된 필드를 볼 수 없습니다.)

답변

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

관련 문제