Skip to content

DECIMAL values are now Strings with mysql2 #7465

Closed

Description

@jaredjenkins

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

Activity

  1. sushantdhiman commented on Apr 1, 2017

    @sushantdhiman
    Contributor

    Decimal needs to be a string, here is a small test case

    parseFloat('12345678123456781.123456781234567');
    12345678123456782

    If we go with your approach we will end up returning wrong data to users. Strings are returned so precision is not lost. I don't think so there is any way around it.

    If you have any solution to problem given above, we can discuss further and work toward it :)

  2. jaredjenkins commented on Apr 1, 2017

    @jaredjenkins
    Author
  3. sushantdhiman commented on Apr 1, 2017

    @sushantdhiman
    Contributor

    @jaredjenkins RIght, it is an undocumented breaking change. I will make sure we have proper tests and document this change in #7471

  4. jaredjenkins commented on Apr 1, 2017

    @jaredjenkins
    Author
  5. sushantdhiman commented on Apr 1, 2017

    @sushantdhiman
    Contributor

    One thing that I can think of to pass an optional flag to automatically
    parse the value. This would be opt-in, only. What do you think?

    Why dont you try FLOAT ?

  6. jaredjenkins commented on Apr 4, 2017

    @jaredjenkins
    Author

    @sushantdhiman I think we'll move to integers instead. Prior developer chose DECIMAL for currency values. Would be cleaner to store "cents" or lowest base currency as integer.

    Thanks for the fast review!

  7. miselaytes-anton commented on May 18, 2017

    @miselaytes-anton

    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}
    });
    
  8. joe-angell commented on Jul 1, 2017

    @joe-angell

    I also ran into this issue upgrading from sequelize 3 and am wondering how to best handle this. In my case changing the datatype is not an option. I think making DECIMAL a string datatype by default is the wrong approach, it is fundamentally a float data type and will be used in float calculations.

    I could see using a library like: https://github.com/MikeMcl/decimal.js/ or some such. Another approach, similar to bigints: if it's too big you convert to a string. The common use case for this datatype is monetary calculations, returning a string sort of renders this datatype useless without additional conversion.

    I can see arguments either way, but dialectOptions: { decimalNumbers: true } will be my workaround. Would recommend documenting that for others.

  9. dengue8830 commented on Aug 8, 2017

    @dengue8830

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions