Repository navigation
Description
Issue Description
On SQL Server, when inserting or updating a table column of type FLOAT with a large float value (> ~50k), the value gets corrupted and the database stores some garbage number.
What are you doing?
Trying to insert large float values into a FLOAT-type column. For example: 123456789.123456
Here's a repo that reproduces this issue.
What do you expect to happen?
The large value is correctly stored on the database.
What is actually happening?
Some garbage value is stored. For the example above, this is stored: 11186.78219
Additional context
I described my journey of encountering this problem on Stack Overflow.
The problem seem to lie here:
sequelize/lib/dialects/mssql/query.js
Lines 24 to 38 in cf5b2f5
| getSQLTypeFromJsType(value, TYPES) { | |
| const paramType = { type: TYPES.VarChar, typeOptions: {} }; | |
| paramType.type = TYPES.NVarChar; | |
| if (typeof value === 'number') { | |
| if (Number.isInteger(value)) { | |
| if (value >= -2147483648 && value <= 2147483647) { | |
| paramType.type = TYPES.Int; | |
| } else { | |
| paramType.type = TYPES.BigInt; | |
| } | |
| } else { | |
| paramType.type = TYPES.Numeric; | |
| //Default to a reasonable numeric precision/scale pending more sophisticated logic | |
| paramType.typeOptions = { precision: 30, scale: getScale(value) }; | |
| } |
The request parameter type is guessed based on the value itself, when I think it should use the model's column type definition. The value itself should be ignored.
This method guarantees that only 4 parameter types are ever allowed, and that seems wrong given all the options available.
For large float values, the Numeric type that is chosen causes the database to receive some corrupted value. When the Float type is chosen, the value arrives perfectly.
Environment
- Sequelize version: 5.21.3
- Node.js version: 8.16.1
- Operating System: Windows 10 Pro 64-bit
Issue Template Checklist
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(s): mssql
- I don't know, I was using mssql
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
sushantdhiman commentedon Apr 9, 2020
This issue has been fixed in master by #11962. I am closing this issue, please let me know if it is still an issue with master
firefueled commentedon Apr 9, 2020
It seems that it is fixed on 04573fd.
Are you planning on integrating that into v5?
sushantdhiman commentedon Apr 9, 2020
No
firefueled commentedon Apr 9, 2020
Would you accept a PR doing so?
sushantdhiman commentedon Apr 9, 2020
Yes
firefueled commentedon Apr 9, 2020
Great!
sushantdhiman commentedon Jun 5, 2020
🎉 This issue has been resolved in version 5.21.12 🎉
The release is available on:
Your semantic-release bot 📦🚀