不要怂,就是干,撸起袖子干!

Commit 5db576a2 by Tiago Bonatti Committed by Sushant

fix(model): sum returns zero when empty matching (#9984)

1 parent ded97557
...@@ -1958,7 +1958,12 @@ class Model { ...@@ -1958,7 +1958,12 @@ class Model {
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
options = this._paranoidClause(this, options); options = this._paranoidClause(this, options);
return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this); return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this).then( value => {
if (value === null) {
return 0;
}
return value;
});;
} }
/** /**
......
...@@ -1136,7 +1136,9 @@ class QueryInterface { ...@@ -1136,7 +1136,9 @@ class QueryInterface {
const dataType = options.dataType; const dataType = options.dataType;
if (dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT) { if (dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT) {
if (!_.isNull(result)) {
result = parseFloat(result); result = parseFloat(result);
}
} else if (dataType instanceof DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) { } else if (dataType instanceof DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
result = parseInt(result, 10); result = parseInt(result, 10);
} else if (dataType instanceof DataTypes.DATE) { } else if (dataType instanceof DataTypes.DATE) {
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
this.Payment = this.sequelize.define('Payment', {
amount: DataTypes.DECIMAL,
mood: {
type: DataTypes.ENUM,
values: ['happy', 'sad', 'neutral']
}
});
return this.sequelize.sync({force: true}).then(() => {
return this.Payment.bulkCreate([
{amount: 5, mood: 'neutral'},
{amount: -5, mood: 'neutral'},
{amount: 10, mood: 'happy'},
{amount: 90, mood: 'happy'}
]);
});
});
describe('sum', () => {
it('should sum without rows', function() {
return expect(this.Payment.sum('amount', {where: {mood: 'sad'}})).to.eventually.be.equal(0);
});
it('should sum when is 0', function() {
return expect(this.Payment.sum('amount', {where: {mood: 'neutral'}})).to.eventually.be.equal(0);
});
it('should sum', function() {
return expect(this.Payment.sum('amount', {where: {mood: 'happy'}})).to.eventually.be.equal(100);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!