Skip to content

Commit 27b83d3

Browse files
sushantdhimanjanmeier
authored andcommitted
Fixed sequelize#4893, DECIMAL precision for Postgres (sequelize#5860)
* test for DECIMAL parser and string default * fixed test cases using Int value for decimal
1 parent a81128b commit 27b83d3

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

Diff for: changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Future
2+
- [FIXED] Postgres DECIMAL precision. (PostgreSQL) [#4893](https://github.com/sequelize/sequelize/issues/4893)
3+
14
# 3.23.0
25
- [FIXED] Invalid query generated when using LIKE + ANY [#5736](https://github.com/sequelize/sequelize/issues/5736)
36
- [FIXED] Method QueryInterface.bulkDelete no longer working when the model parameter is missing. (PostgreSQL) [#5615](https://github.com/sequelize/sequelize/issues/5615)

Diff for: lib/dialects/postgres/data-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function (BaseTypes) {
3636
var DECIMAL = BaseTypes.DECIMAL.inherits();
3737

3838
DECIMAL.parse = function (value) {
39-
return parseFloat(value);
39+
return value;
4040
};
4141

4242
// numeric

Diff for: test/integration/data-types.test.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
307307

308308
if (dialect === 'postgres' || dialect === 'sqlite') {
309309
// postgres actively supports IEEE floating point literals, and sqlite doesn't care what we throw at it
310-
it('should store and parse IEEE floating point literals (NaN and Infinity', function () {
310+
it('should store and parse IEEE floating point literals (NaN and Infinity)', function () {
311311
var Model = this.sequelize.define('model', {
312312
float: Sequelize.FLOAT,
313313
double: Sequelize.DOUBLE,
@@ -330,4 +330,38 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
330330
});
331331
});
332332
}
333+
334+
if (dialect === 'postgres') {
335+
it('should parse DECIMAL as string', function () {
336+
var Model = this.sequelize.define('model', {
337+
decimal: Sequelize.DECIMAL,
338+
decimalPre: Sequelize.DECIMAL(10, 4),
339+
decimalWithParser: Sequelize.DECIMAL(32, 15),
340+
decimalWithIntParser: Sequelize.DECIMAL(10, 4),
341+
decimalWithFloatParser: Sequelize.DECIMAL(10, 8)
342+
});
343+
344+
var sampleData = {
345+
id: 1,
346+
decimal: 12345678.12345678,
347+
decimalPre: 123456.1234,
348+
decimalWithParser: '12345678123456781.123456781234567',
349+
decimalWithIntParser: 1.234,
350+
decimalWithFloatParser: 0.12345678
351+
};
352+
353+
return Model.sync({ force: true }).then(function () {
354+
return Model.create(sampleData);
355+
}).then(function () {
356+
return Model.find({id: 1});
357+
}).then(function (user) {
358+
expect(user.get('decimal')).to.be.eql('12345678.12345678');
359+
expect(user.get('decimalPre')).to.be.eql('123456.1234');
360+
expect(user.get('decimalWithParser')).to.be.eql('12345678123456781.123456781234567');
361+
expect(user.get('decimalWithIntParser')).to.be.eql('1.2340');
362+
expect(user.get('decimalWithFloatParser')).to.be.eql('0.12345678');
363+
});
364+
});
365+
}
366+
333367
});

Diff for: test/integration/dialects/postgres/dao.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ if (dialect.match(/^postgres/)) {
645645
// Check to see if the default value for a range field works
646646

647647
expect(newUser.acceptable_marks.length).to.equal(2);
648-
expect(newUser.acceptable_marks[0]).to.equal(0.65); // lower bound
649-
expect(newUser.acceptable_marks[1]).to.equal(1); // upper bound
648+
expect(newUser.acceptable_marks[0]).to.equal('0.65'); // lower bound
649+
expect(newUser.acceptable_marks[1]).to.equal('1'); // upper bound
650650
expect(newUser.acceptable_marks.inclusive).to.deep.equal([false, false]); // not inclusive
651651
expect(newUser.course_period[0] instanceof Date).to.be.ok; // lower bound
652652
expect(newUser.course_period[1] instanceof Date).to.be.ok; // upper bound
@@ -717,8 +717,8 @@ if (dialect.match(/^postgres/)) {
717717
return User.create({ username: 'user', email: ['[email protected]'], course_period: period }).then(function(newUser) {
718718
// Check to see if the default value for a range field works
719719
expect(newUser.acceptable_marks.length).to.equal(2);
720-
expect(newUser.acceptable_marks[0]).to.equal(0.65); // lower bound
721-
expect(newUser.acceptable_marks[1]).to.equal(1); // upper bound
720+
expect(newUser.acceptable_marks[0]).to.equal('0.65'); // lower bound
721+
expect(newUser.acceptable_marks[1]).to.equal('1'); // upper bound
722722
expect(newUser.acceptable_marks.inclusive).to.deep.equal([false, false]); // not inclusive
723723
expect(newUser.course_period[0] instanceof Date).to.be.ok;
724724
expect(newUser.course_period[1] instanceof Date).to.be.ok;

0 commit comments

Comments
 (0)