You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.sum on an empty matching set results in NaN #6299
I'm trying to use the SUM aggregate on an empty matching set
//imagine we have Payments table empty yetPayment.sum('amount').then(console.log)//logs NaN
What do you expect to happen?
I'd like the value in this case to be 0 since by definition, a sum is a number, not a NaN.
Why this happens?
I've quickly found that this happens due to this:
//node_modules/sequelize/lib/query-interface.js:702//result contains null as we don't have any matching recordsresult=parseFloat(result);
What could be done?
since the dataType requested is dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT as stated in node_modules/sequelize/lib/query-interface.js:701, we can additionally check whether the result variable contains null and in this case return 0. I can submit a PR with this fix easily though I'd first like to ensure that I'm not the only one expecting 0 in this case rather than NaN.
Activity
sushantdhiman commentedon Jul 18, 2016
Sequelize version ? I remember fixing this issue, I think we already have test for something like this.
Edit: I remembered fixing this for
countbut I think otheraggregatemethods still have this issue. I will need Sequelize + DB version @yktv4yktv4 commentedon Jul 18, 2016
@sunshinewyin sorry for not providing it initially. here it is:
3.19.3janudewangga commentedon Mar 14, 2018
Is it fixed yet? I just had a same problem in
4.36.1Fixed aggregateBlockReward little bug when no matching stuff due to s…
bminer commentedon Sep 11, 2018
This is silly IMHO.
sumshould returnnullor 0. NotNaN.jdestep93 commentedon Sep 11, 2018
I agree with @bminer
yktv4 commentedon Sep 11, 2018
I guess mentioning any core developer could help. anyone knows their handles?