Knex.js Error parseLengthCodedNumber: JS precision range exceeded, number is >= 53 bit: "18446744073709551615"

Viewed 246

I am trying to insert a few rows to a table from an express.js controller, using knex.js:

yield knex.withSchema(MySchema).insert(something).into('MyTable');

but I get this error:

Error: parseLengthCodedNumber: JS precision range exceeded, number is >= 53 bit: "18446744073709551615"

The 'something' varible is a set of rows from another table, which have negative ids:

'-2', '552', 'something1', NULL, '-2', '1', '0000-00-00 00:00:00', '2020-03-31 14:27:32'
'-1', '552', 'something2', NULL, '-1', '0', '0000-00-00 00:00:00', '0000-00-00 00:00:00'

My database is MariaDB 10.3.21 and I'm using knex 0.12.9

Any ideas?

1 Answers

I believe in MySQL .increments() will default to unsigned. I haven't tested it, but I expect attempting to insert a negative integer will result in an overflow (see MySQL integer overflow for exact behaviour). This appears to be resulting in a positive integer that is greater than JavaScript's MAX_SAFE_INTEGER.

If you absolutely must have negative IDs, you could probably work around it by setting the table to use signed integers as primary keys, importing the data, then setting the autoincrement starting value to something greater than the highest id represented in the data. I imagine all of your tables would need signed integers as IDs so you're comparing like with like.

Related