Whenever I try to insert a floating point number larger than ~50k using the sequelize ORM, the database gets populated with something else.
For this value 123456789.123456
I get this inserted 11186.78219
.
Some other colleagues have gotten different outputs, but close to 11186.78219
like 11177.782191683797
, for other inputs. I'm not sure what that hints at.
Everything works fine when I perform the same insert via the node-mssql
client, which is also used by Sequelize internally
So I traced the execution path from the Sequelize's model's create statement, through node-mssql
, through tedious, and up to the node.js network request libraries just to see that my initial input number was kept unchanged throughout.
After the request is sent, though, something happens and the database receives a different value.
Setting a large value directly on the database and retrieving it through Sequelize works fine, as well.
I created this repo to see if any of you can replicate it.
Edit: I guess I should clarify how I'm using Sequelize :)
This an example User model, and creation, that fails:
const User = sequelize.define('User', {
networth: {
type: DataTypes.FLOAT,
// none of these work
// type: DataTypes.FLOAT(20, 5),
// type: DataTypes.DECIMAL(20, 5),
// type: DataTypes.DOUBLE,
},
}, {
timestamps: false,
});
User.create({ networth: 123456798.123456 });
This node-mssql
example works, though:
await pool.request()
.input('networth', sql.Float, float_value)
.query(`insert into users (networth) values (@networth)`)
Edit 2: I grabbed some data from SQL Server Profiler showing the differences between the Sequelize and node-mssql
calls:
node-mssql: exec sp_executesql @statement=N'insert into users (networth) values (@networth)',@params=N'@networth float',@networth=123456798,123456
Sequelize: exec sp_executesql @statement=N'INSERT INTO [Users] ([networth]) OUTPUT INSERTED.* VALUES (@0);',@params=N'@0 numeric(30, 15)',@0=11186.782191684157440
So the data does arrive broken...
Edit 3: I think I found the source of the problem and I opened an issue here.
So it is a bug on Sequelize v5 and it is resolved on the master branch. I plan to submit a PR to have it integrated into v5, as well.