I have two models, campaign and donation. The campaign model has many donations. I am trying to run a query against the campaigns model to get a campaign by id including the donations associated with that campaign in addition to the sum and count of the donations.
Here's the query that I'm using:
Campaign.findById(campaignId,
{
include: [
{
model: Donations
}
],
attributes: {
include: ['Campaign.id', [
sequelize.fn('COUNT', sequelize.col('Donations.id')), 'numberOfDonations',
sequelize.fn('SUM', sequelize.col('Donations.amount')), 'amountRaised'
]]
},
group: [ 'Campaign.id', 'Donations.id' ]
}
).then(function(campaign) {
resolve(campaign);
})
Which is throwing the error Error: TypeError: s.replace is not a function
However, when I remove one of the sequelize.fn(...)
attributes and keep one, it works exactly as expected. It's only letting me get either the count or the the sum of the donations with the campaign.
I am using sequelize version 3.19.3 with postgresql.
Smethings off with your attributes.include array, each of your fn/alias pairs should be an array, not an array around two sets.
attributes: {
include: [
'id',
[sequelize.fn('COUNT', sequelize.col('Donations.id')), 'numberOfDonations',]
[sequelize.fn('SUM', sequelize.col('Donations.amount')), 'amountRaised']
]
}
That worked! Thank you very much for the very quick reply!
Most helpful comment