2017-11-26 1 views
0

여기 내 모델과 그 관계. 내가 달성하고자하는 지불의 합은 각각의 수수료 후 낮은 곳에 나는 plate_id와 일치하는 영수증을 발견하고 영수증을 발견 할 것입니다Sequelize.JS Postgresql - 마스터 테이블의 필드를 비교하기 위해 연결된 테이블에서 SUM을 계산하는 방법

// 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' });
다음

, 영수증.

// db.Op.lt 즉, 같은 우려,이 일을하는 방법 중 하나 여기 방법이있을 수 있습니다 사람들을 위해

답변

0

db.Receipts.findAll({ 
 
     where : { 
 
      plate_id : result.plate_id, 
 
      fee : { [ db.Op.lt ] : db.Payments.sum('receivedPayment')} 
 
     }, 
 
     include : [db.Receipts.associations.Payments, 
 
      db.Receipts.associations.ReceiptItems, 
 
     ] 
 
     }).then((receiptResult)=>{ 
 
     console.log("result"+JSON.stringify(receiptResult)); 
 
     }).catch((receiptErr)=>{ 
 
     console.log(receiptErr); 
 
     })
"보다". 당신이 시도 후

db.Receipts.findAll({ 
 
    group: ['Receipts.receipt_id', 'ReceiptFees->User.user_id', 'ReceiptPayments->User.user_id'], 
 
    attributes: [ 
 
     [db.sequelize.fn('SUM', db.sequelize.col('ReceiptFees.fee')), 'totalFee'], 
 
     [db.sequelize.fn('SUM', db.sequelize.col('ReceiptPayments.receivedPayment')), 'totalPayment'] 
 
    ], 
 
    include: [ 
 
     { 
 
     model: db.ReceiptFees, 
 
     attributes: [], 
 
     include: [ 
 
      { association: db.ReceiptFees.associations.User }, 
 
     ] 
 
     }, 
 
     { 
 
     model: db.ReceiptPayments, 
 
     attributes: [], 
 
     include: [ 
 
      { association: db.ReceiptPayments.associations.User }, 
 
     ] 
 
     } 
 
    ], 
 
    }).then(res=> // do your work...)

나에게 의견을 보내 주시기 바랍니다.

관련 문제