Using group by and joins in sequelize
This issue has been fixed on Sequelize 3.0.1, the primary key of the included models must be excluded with
attributes: []
and the aggregation must be done on the main model (infos in this github issue).
Thus for my use case, the code is the following
models.contracts.findAll({ attributes: ['id', [models.sequelize.fn('sum', models.sequelize.col('payments.payment_amount')), 'total_cost']], include: [ { model: models.payments, attributes: [] } ], group: ['contracts.id']})
Can you write your function as
models.contracts.findAll({ attributes: [ 'models.contracts.id' ], include: [ { model: models.payments, attributes: [[models.sequelize.fn('sum', models.sequelize.col('payments.payment_amount')), 'total_cost']] } ], group: ['contracts.id']})