You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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(){constModel=this.sequelize.define('model',{jewelPurity: Sequelize.DECIMAL(10,2)});constsampleData={id: 1,jewelPurity: 133.7,};returnModel.sync({force: true}).then(()=>{returnModel.create(sampleData);}).then(()=>{returnModel.findById(1);}).then(user=>{debuggerexpect(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
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 :)
Ok but this is a huge undocumented breaking change with the current
library. In v3 you get back a number, in v4 it's a string. How is Sequelize
planning to deal with the upgrade?
@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.
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.
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.
this is a serius problem for javascript in the server :/
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?
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 ?
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?
Reacted by Reza Daliri, Ruofan Wei and Uros Cirkovic
Activity
sushantdhiman commentedon Apr 1, 2017
Decimal needs to be a string, here is a small test case
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 :)
jaredjenkins commentedon Apr 1, 2017
sushantdhiman commentedon Apr 1, 2017
@jaredjenkins RIght, it is an undocumented breaking change. I will make sure we have proper tests and document this change in #7471
jaredjenkins commentedon Apr 1, 2017
sushantdhiman commentedon Apr 1, 2017
Why dont you try
FLOAT?jaredjenkins commentedon Apr 4, 2017
@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!
miselaytes-anton commentedon May 18, 2017
Hi,
in my case I do
sequelize.query('select sum(steps) as steps from measurement' )where steps issteps: {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?
joe-angell commentedon Jul 1, 2017
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.dengue8830 commentedon Aug 8, 2017
this is a serius problem for javascript in the server :/
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?something like this:
2.1) but how to save this extra big numbers?
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?