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.

DECIMAL values are now Strings with mysql2

See original GitHub issue

What are you doing?

Hi I recently needed to upgrade to v4.0.0-2 to get a critical bug fix. In the process of the upgrade, I needed to also bump to mysql2. I’m getting close to wrapping things up but I found something peculiar with the way that DECIMALs are now handled.

Per this issue in the mysql2 project, DECIMAL is now returned as a String not a number. Their proposed fix is for the consumer to apply a typeParser to the mysql2 connection.

Here is a simple test that fails:

it('should parse the decimal as a number', function(){
      const Model = this.sequelize.define('model', {
        jewelPurity: Sequelize.DECIMAL(10, 2)
      });

      const sampleData = {
        id: 1,
        jewelPurity: 133.7,
      };

      return Model.sync({ force: true }).then(() => {
        return Model.create(sampleData);
      }).then(() => {
        return Model.findById(1);
      }).then(user => {
        debugger
        expect(user.jewelPurity).to.not.be.string;
        expect(user.jewelPurity).to.be.eql(sampleData.jewelPurity);
      });
 });

What do you expect to happen?

I expect the numerical fields to continue to behave consistently after the upgrade.

What is actually happening?

I get back strings which is surprising.

Dialect: mysql Database version: 5.6 Sequelize version: 4.0.0-2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
miselaytes-antoncommented, May 18, 2017

Hi, in my case I do sequelize.query('select sum(steps) as steps from measurement' ) where steps is steps: {type: DataTypes.INTEGER} and get steps as a string, while I prefer to have it as an integer.

Is it safe to fix it like below?

const sequelize = new Sequelize(config.sql.database, config.sql.username, config.sql.password, {
  dialect: 'mysql',
  dialectOptions: {decimalNumbers: true}
});
3reactions
dengue8830commented, Aug 8, 2017

this is a serius problem for javascript in the server 😕

  1. if the problem is inherent of the javascript number representation, a downgrade to sequelize v3 or the dialect trick is not a solution because this is a problem that has been there forever, am i right?

1.1) if i have sequelize v3, my extra-big numbers will be rounded with no solution?

1.2) and the dialect trick dialectOptions: { decimalNumbers: true } is a bad idea too because it’s like use v3, the extra-big numbers will be rounded?

  1. The best solution is use FLOAT for numbers that we know that can be successfully parsed to a js number, and DOUBLE with a manual parse to a third lib that handles extra-big numbers, am i right?

something like this:

let company = await Company.findById(1);
company.extrabignumber = new Decimal(company.extrabignumber);
company.extrabignumber.add(99999999999999);

2.1) but how to save this extra big numbers?

company.extrabignumber = company.extrabignumber.toString();
company.save(); // could this save the number successfully ?
  1. Could sequelize include some lib to handle this numbers and prevent the manual process described above?

so, sequelize v4 doesn’t gave us a new problem, only shows us a problem that has been there for a long time. Can somebody help with this doubts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why MySQL decimal fields are retrieved like strings while ...
Today I've observed this weird thing while programming, now I've created a new ... The former value comes as a string while the...
Read more >
Why MYSQL is giving Double data type value as string in ...
I am trying to get a double data type value from Mysql in Nodejs, but that value is coming as String ...
Read more >
11.1.3 Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important ... MySQL stores DECIMAL values...
Read more >
MySQL DECIMAL Data Type
MySQL assigns the storage for integer and fractional parts separately. MySQL uses binary format to store the DECIMAL values. It packs 9 digits...
Read more >
Why does Casting this string as a decimal fail?
This has been a long-standing issue with MySQL with people reporting this ... FORMAT(expression, 2) -- for displaying with 2 decimal places ......
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