question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize returns a string for decimal types from postgres

See original GitHub issue

It’s returning strings for my decimal data types

I have a model with a DECIMAL column. Whenever I query that model the column is returned as a string.

When i try to get data from my postgres DB

app.get('/itens', function(req, res) {
	models.itens.findAll().then(function(itens) {
		res.json(itens);
	});
});

It supposed to return a float value

The return always is a String value

{
	"price": "196",
}

I am using

  • Sequelize version: 5.21.2
  • Node.js version: 10.15.3
  • Operating System: mac Os

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect:
  • I don’t know, I was using postgres, with connector library version 7.12.1 and database version 12

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
hcurnorcommented, Mar 26, 2021

Suggested workaround

sequelize.define("user", {
  foo: {
    type: Sequelize.DECIMAL,
    get() {
      const value = this.getDataValue('foo');
      return value === null ? null : parseFloat(value);
    }
  }
})

0reactions
ephyscommented, Apr 16, 2022

This is the expected behavior for decimal, you should use float or double if you want to use the number type in javascript

Nonetheless, https://github.com/sequelize/sequelize/issues/14296 proposes a way to return number out of the box

I’ll close as this issue in favor of https://github.com/sequelize/sequelize/issues/8019

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize returns a string for decimal types #8019 - GitHub
I have a simple model with a DECIMAL column. Whenever I query that model the column is returned as a string rather than...
Read more >
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 ...
I don't know why it isn't stated in sequelize v4 docs, but v3 states that: An explicit cast is required on postgres. http://sequelize....
Read more >
Data Types - Sequelize
DECIMAL (precision, scale) is meant to be a constrained decimal type. All, PostgreSQL, MariaDB, MySQL, MSSQL, SQLite, Snowflake, db2, ibmi. Sequelize DataType ...
Read more >
Sequelize decimal type error - maciek ☁️
Sequelize returns a string instead of a number for decimal type.
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