Returns wrong BigInt insertIds
See original GitHub issueDB Schema
CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12345678901234567945 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
MYSQL Config
{
host: 'localhost',
user: 'develop',
password: 'develop',
database: 'develop',
waitForConnections: true,
namedPlaceholders: true,
connectionLimit: 1,
bigNumberStrings: true,
supportBigNumbers: true,
};
QUERY
INSERT INTO users(name) VALUES('test')
Response
[
ResultSetHeader {
fieldCount: 0,
affectedRows: 1,
insertId: '-6101065172474983670',
info: '',
serverStatus: 2,
warningStatus: 0
},
undefined
]
Version: ^2.3.0
PS: Works as expected with https://github.com/mysqljs/mysql
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Mysql returning incorrect bigint result by one, very strange error
With this data: · When I run this query SELECT * FROM game WHERE id = 4 in phpmyadmin I get back this...
Read more >Node.js with MySQL - w3resource
... bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be always returned as JavaScript String objects (Default: false).
Read more >PDO::lastInsertId - Manual - PHP
PDO::lastInsertId — Returns the ID of the last inserted row or sequence value ... "With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) ......
Read more >MySQL BIGINT return (sometimes) wrong value - Xojo Forum
Hi there, I've debugging for hours and struggling with a BigInt column in MySQL which (sometimes) returns wrong value.
Read more >[CONJS-193] Wrong error returned "Cannot read properties of ...
Wrong error returned "Cannot read properties of undefined… ... console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }. } catch (err) {....
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
This was a MySQL bug 10 years ago https://bugs.mysql.com/bug.php?id=20964
Are you setting an initial
AUTO_INCREMENT
value when creating the table? Is it aBIGINT UNSIGNED
column like the OP example?That error should not be related to the fact that you insert a negative number. I think that only happens when the
AUTO_INCREMENT
value is already out of range for the given column data type.For instance, using OP’s example but instead making the
id
column aBIGINT SIGNED
(mind that by omission, numeric columns areSIGNED
), it will fail with that same error because the initialAUTO_INCREMENT
value is already out-of-range. So, if you rely on it whilst inserting anything and not providing a specificid
value, it will yield that error.The problem with your example is that, if the column data type is
SIGNED
, negative numbers are valid, so there’s no reason to not allow that.Of course if the column data type is
UNSIGNED
then you already get an error if you try toINSERT
negative values.The only problem here is that
insertId
creates false expectations about what it returns. To make matters worse,LAST_INSERT_ID()
also does not help. Both those values are only reliable if you don’t source your own values whileINSERT
ing.I’ve talked with a few people (working with client implementations) about it and some of them were not even aware of this kind of behaviour, which means it probably hasn’t been an issue. Maybe that’s because the behaviour is documented somewhere (couldn’t find anything).
So, the idea that I got was that we probably need to live with it. And to be honest, I don’t see any kind of limitation here as well. When your
INSERT
relies on values “generated” byAUTO_INCREMENT
, theinsertId
is fine. When you provide your own values, you already know them, so there’s no point in looking for whatever is returned byinsertId
. It might be a bit more tricky for bulk inserts, but the reasoning still stands.