Sequelize returns a string for decimal types

See original GitHub issue

I have a simple model with a DECIMAL column. Whenever I query that model the column is returned as a string rather than a float. Thing is, I am not sure whether the issue is on ExpressJS side (res.json()) or on Sequelize side. Or maybe there’s something else I am not aware of.

app.get('/test/list/do', function(req, res) {
	models.test.findAll().then(function(tests) {
		res.json(tests);
	});
});

I am using the latest version of Sequelize and ExpressJS along with MySQL 5.7.

EDIT: I tried logging the model directly in Node and the field was already converted to a string before sending it out. So it must be Sequelize.

I understand it’s probably due to precision issues but I would like to know if there’s a away to get a number regardless.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:42
  • Comments:47 (9 by maintainers)

github_iconTop GitHub Comments

80reactions
deejbeecommented, Apr 25, 2018

I finally found a way and ended up doing this with the latest (4.37.6)

Sequelize.postgres.DECIMAL.parse = function (value) { return parseFloat(value); };

using setTypeParser() had no effect.

Since I know all my values are currency style decimals there hopefully won’t be a problem losing any precision. I wish there was a different type of DECIMAL (DECIMAL_2 or DECIMAL_INSECURE perhaps?)

74reactions
nerdlibfrontcommented, Jul 31, 2017

This is an issue with mysql2 returning decimals as a string to preserve precision: https://github.com/sequelize/sequelize/issues/7465

If you are sure that your data won’t run into precision issues you can set dialectOptions: { decimalNumbers: true } when instantiating sequelize to get the previous behavior. Worked for my case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres/Sequelize — returns a string for decimal/numeric type
Here I found a work around to make it returns as string in the code base, and it worked well for me.
Read more >
why is my sequelize query that sums a decimal column ...
discountPercentage: { type: DataTypes.DECIMAL(18, 3), allowNull: true, defaultValue: 0.000, get() { return parseFloat(this.
Read more >
Data Types - Sequelize
DECIMAL (precision, scale) is meant to be a constrained decimal type. ... we recommend using string to represent Exact Decimal Numbers in JavaScript....
Read more >
Sequelize decimal type error - maciek ☁️
Sequelize returns a string instead of a number for decimal type.
Read more >
Datatypes - Manual | Sequelize
The BLOB datatype allows you to insert data both as strings and as buffers. When you do a find or findAll on a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found