DECIMAL values are now Strings with mysql2
See original GitHub issueWhat 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:
- Created 6 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
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?