Skip to content

Large float values get corrupted because of incorrect request parameter type setting #12086

Closed

Description

@firefueled

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:

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

  1. sushantdhiman commented on Apr 9, 2020

    @sushantdhiman
    Contributor

    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

  2. firefueled commented on Apr 9, 2020

    @firefueled
    Author

    It seems that it is fixed on 04573fd.
    Are you planning on integrating that into v5?

  3. sushantdhiman commented on Apr 9, 2020

    @sushantdhiman
    Contributor

    No

  4. firefueled commented on Apr 9, 2020

    @firefueled
    Author

    Would you accept a PR doing so?

  5. sushantdhiman commented on Apr 9, 2020

    @sushantdhiman
    Contributor

    Yes

  6. firefueled commented on Apr 9, 2020

    @firefueled
    Author

    Great!

  7. sushantdhiman commented on Jun 5, 2020

    @sushantdhiman
    Contributor

    🎉 This issue has been resolved in version 5.21.12 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions