You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
If a BigInt/Int field exists on update, the record is not updated. #1483
A bit difficult for me to follow original sequelize discussion. Can anyone prepare small self-contained example only using this driver to demonstrate the issue? ( Ideally as a failing unit test, but just a script with schema preparation + query + assertion is also enough )
I created a small javascript test file with succeeding and failing tests to demonstrate the problem. In the following example, test 1 and 4 should work correctly, and test 2 and 3 should fail to change the int field. For some reason, anything above 16 digits doesn't work for the bigint field. I'm running on WSL2 Ubuntu 20.04 and mysql version 8.0.27-0ubuntu0.21.10.1
schema.sql
CREATEDATABASEIF NOT EXISTS test
USE test;
CREATETABLEIF NOT EXISTS Example (
IntField int,
BigIntField bigint(18)
);
INSERT INTO Example (IntField, BigIntField) VALUES (123, 8666079548394975); -- 16 digitsINSERT INTO Example (IntField, BigIntField) VALUES (1337, 86660795483949752); -- 17 digits
index.js
const{ createConnection }=require('mysql2/promise');(async()=>{try{constconn=awaitcreateConnection({host: 'localhost',user: 'root',password: 'root',database: 'test',supportBigNumbers: true,bigNumberStrings: true});awaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[5,8666079548394975n]);const[first]=awaitconn.execute('SELECT * FROM Example WHERE BigIntField = ?',[8666079548394975n]);console.log('test #1',first[0].IntField===5,first);// supposed to return true and it doesawaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[10,86660795483949752n]);const[second]=awaitconn.execute('SELECT * FROM Example WHERE BigIntField = ?',[86660795483949752n]);console.log('test #2',second[0].IntField===10,second);// supposed to return true but it doesn'tawaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',['15','86660795483949752']);const[third]=awaitconn.execute('SELECT * FROM Example WHERE BigIntField = ?',['86660795483949752']);console.log('test #3',third[0].IntField===15,third);// supposed to return true but it doesn'tawaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[20,'8666079548394975']);const[fourth]=awaitconn.execute('SELECT * FROM Example WHERE BigIntField = ?',['8666079548394975']);console.log('test #4',fourth[0].IntField===20,fourth);// supposed to return true and it doesawaitconn.end();process.exit(0);}catch(err){console.error(err);process.exit(1);}})();
Currently bigint ( typeof parameter === 'bigint' ) is serialised as VAR_STRING here.
What I don't understand is why WHERE ( with implicit internal type cast ) works differently for SELECT vs UPDATE
in a table with BigIntField: '86660795483949752' bigint(18) row doing
const[row]=awaitconn.execute('SELECT * from Example WHERE BigIntField = ?',[86660795483949752n]);
yields 1 row as expected, while
awaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[10,86660795483949752n]);
does not update expected row ( 'Rows matched: 0 Changed: 0 Warnings: 0' )
One possible guess: when sent as string in case of SELECT WHERE the results are cast to string and then compared with "86660795483949752" parameter while in case of UPDATE WHERE the parameter goes via number -> string conversion ?
// this updates 0 columnsawaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[10,86660795483949752n]);// this does update one expected columnawaitconn.execute('UPDATE Example SET IntField = 10 WHERE BigIntField = ?',[86660795483949752n]);
Having both variables as a string worked when I was developing my Discord bot, but looking back at the tests, that suggests the opposite. Try it, but I can't guarantee that it'll work.
Currently bigint ( typeof parameter === 'bigint' ) is serialised as VAR_STRING here.
What I don't understand is why WHERE ( with implicit internal type cast ) works differently for SELECT vs UPDATE
in a table with BigIntField: '86660795483949752' bigint(18) row doing
const[row]=awaitconn.execute('SELECT * from Example WHERE BigIntField = ?',[86660795483949752n]);
yields 1 row as expected, while
awaitconn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?',[10,86660795483949752n]);
does not update expected row ( 'Rows matched: 0 Changed: 0 Warnings: 0' )
One possible guess: when sent as string in case of SELECT WHERE the results are cast to string and then compared with "86660795483949752" parameter while in case of UPDATE WHERE the parameter goes via number -> string conversion ?
For some reason, I only noticed my mention today given the latest comment. I'll give it a look and see if I'm able to understand what the problem might be. I recall some changes with regards to the length field for integers (which was always kind of "informal" BTW).
The issue is only reproducible with prepared statements starting with MySQL 8.0.22 and seems to be the same as the one identified by #1239. In this case, it seems that, luckily enough, the server still somehow casts the value for the SELECTstatement, but it does not follow up with the UPDATE statement. So the answer is, sort of, the same. Until the driver starts using the types being hinted in the response to the PREPARE statement, these kind of issues will surface.
@sidorares I remember you mentioned that you started working on a patch for it. If that is not the case, I'll gladly try to write one myself.
@ruiquelhas I have this PR #1483 but there type hint is always respected and I think there are many scenarios where type different from hint should be used instead. Sometimes server just doesn't have a way to get a type hint ( SELECT ? as response )
Possible solutions:
have some exception for default types based on the input ( JS Date always serialised as DATETIME for example )
api to communicate desired parameter type and override hint: execute(sql, [mysql.DECIMAL('123,45')])
would be really good to create a table with different combination of hint and input types: statement example, reported type hints, type of parameter, ideal type for execute
Right, I think the best is probably to have both. A default type, which is somehow internally mapped and an API to override that default value with whatever the users want, at their own peril.
I agree with @ruiquelhas, I think doing the ability to override will be a nice feature. I would be doing this myself as I do have specific datatypes that convert to specific types. I also do not use JSON.parse/JSON.stringify because it does not have bigint support I use json-bigint npm for this purpose. So the ability to convert a JSON type to bigint parse/stringify will be very nice.
It's almost been 1 year since any progress, can we get an update on this bug? This is actually a breaking change when I cannot properly use this library with sequelize.
where the id is a BIGINT number If I copy this with the actual values and do it in MySQL Workbench. Now if I run a query DIRECTLY without using the mysql2 replacement variables it will work.
If I use pure connection.query instead of connection.execute it runs and works as expected. Even if I convert the bigint to an String value using this code ...
awaitconn.execute('UPDATE `address` SET `line2`=?,`lat`=?,`long`=? WHERE `id` = ?',["123",36.5854828,-82.3511855,"1569735910751208448"]);
awaitconn.execute('UPDATE `address` SET `line2`=?,`lat`=?,`long`=? WHERE `id` = ?',["123",36.5854828,-82.3511855,1569735910751208448n]);
Neither of these update the record.
So I have to resort to using a custom query that uses replacement variables and not use any of the "bind parameters" which seems to be the problem.
I am using AWS RDS 8.0.mysql_aurora.3.04.0 which is MySQL 8.0.28 version
However after doing some research on the MySQL version changes, it seems to me that this whole parameter issue could fail to update a row is in fact an engine bug.
MySQL 8.0.31 bug related I think
A prepared statement with parameters could fail to update a row, but the same statement with the same data did update the row when issued as a query. The fix for the problem is to assign a default data type to the parameters, although this can be inefficient because there is no context available for data type propagation and a character string type is given implicitly. In this case, the best practice is to wrap such parameter declarations in CAST clauses that supply the desired data types. (Bug #33521861)
I will update my staging environment to 8.0.34 (Which is the one that is highest available on AWS) and let you know if this is something we can finally close the case on.
Activity
CarelessInternet commentedon Jan 7, 2022
Same problem here, can't update the field int value where the other field value is of bigint type
sidorares commentedon Jan 13, 2022
A bit difficult for me to follow original sequelize discussion. Can anyone prepare small self-contained example only using this driver to demonstrate the issue? ( Ideally as a failing unit test, but just a script with schema preparation + query + assertion is also enough )
CarelessInternet commentedon Jan 20, 2022
I created a small javascript test file with succeeding and failing tests to demonstrate the problem. In the following example, test 1 and 4 should work correctly, and test 2 and 3 should fail to change the int field. For some reason, anything above 16 digits doesn't work for the bigint field. I'm running on
WSL2 Ubuntu 20.04and mysql version8.0.27-0ubuntu0.21.10.1schema.sqlindex.jsmicrovn commentedon Feb 9, 2022
up.
CarelessInternet commentedon Mar 30, 2022
@sidorares any updates?
sidorares commentedon Mar 31, 2022
I might need @ruiquelhas help here.
Currently bigint (
typeof parameter === 'bigint') is serialised as VAR_STRING here.What I don't understand is why
WHERE( with implicit internal type cast ) works differently for SELECT vs UPDATEin a table with
BigIntField: '86660795483949752'bigint(18) row doingyields 1 row as expected, while
does not update expected row (
'Rows matched: 0 Changed: 0 Warnings: 0')One possible guess: when sent as string in case of
SELECT WHEREthe results are cast to string and then compared with"86660795483949752"parameter while in case ofUPDATE WHEREthe parameter goes via number -> string conversion ?sidorares commentedon Mar 31, 2022
hm, what's even more strange:
Uniphix commentedon Jul 27, 2022
I have this same issue right now and it is actually hindering my project with work because of it.
Uniphix commentedon Jul 27, 2022
Is there a current work around with this?
CarelessInternet commentedon Jul 28, 2022
Having both variables as a string worked when I was developing my Discord bot, but looking back at the tests, that suggests the opposite. Try it, but I can't guarantee that it'll work.
microvn commentedon Jul 29, 2022
The problem is still unresolved. I am not knowledgeable enough to improve this project
ruiquelhas commentedon Jul 29, 2022
For some reason, I only noticed my mention today given the latest comment. I'll give it a look and see if I'm able to understand what the problem might be. I recall some changes with regards to the length field for integers (which was always kind of "informal" BTW).
sidorares commentedon Jul 29, 2022
thanks @ruiquelhas
I suspect #1483 (comment) can be replicated in any client and is a feature/bug of a mysql server
ruiquelhas commentedon Jul 29, 2022
The issue is only reproducible with prepared statements starting with MySQL 8.0.22 and seems to be the same as the one identified by #1239. In this case, it seems that, luckily enough, the server still somehow casts the value for the
SELECTstatement, but it does not follow up with theUPDATEstatement. So the answer is, sort of, the same. Until the driver starts using the types being hinted in the response to thePREPAREstatement, these kind of issues will surface.@sidorares I remember you mentioned that you started working on a patch for it. If that is not the case, I'll gladly try to write one myself.
sidorares commentedon Jul 29, 2022
@ruiquelhas I have this PR #1483 but there type hint is always respected and I think there are many scenarios where type different from hint should be used instead. Sometimes server just doesn't have a way to get a type hint (
SELECT ? as response)Possible solutions:
execute(sql, [mysql.DECIMAL('123,45')])would be really good to create a table with different combination of hint and input types: statement example, reported type hints, type of parameter, ideal type for execute
ruiquelhas commentedon Jul 30, 2022
Right, I think the best is probably to have both. A default type, which is somehow internally mapped and an API to override that default value with whatever the users want, at their own peril.
Uniphix commentedon Aug 5, 2022
I agree with @ruiquelhas, I think doing the ability to override will be a nice feature. I would be doing this myself as I do have specific datatypes that convert to specific types. I also do not use JSON.parse/JSON.stringify because it does not have bigint support I use json-bigint npm for this purpose. So the ability to convert a JSON type to bigint parse/stringify will be very nice.
Uniphix commentedon Sep 15, 2022
Just curious on how this fix progressing?
Uniphix commentedon Aug 7, 2023
It's almost been 1 year since any progress, can we get an update on this bug? This is actually a breaking change when I cannot properly use this library with sequelize.
hoangmf commentedon Aug 8, 2023
@Uniphix I think it was abandoned. try wrapping with string. Hope it helps you
Uniphix commentedon Nov 8, 2023
This still doesn't work, I wrapped it as a string and it will not update. The LOG shows the Update Statement example:
where the id is a BIGINT number If I copy this with the actual values and do it in MySQL Workbench. Now if I run a query DIRECTLY without using the mysql2 replacement variables it will work.
Uniphix commentedon Mar 18, 2024
This still will not work with 3.9.2. It calls the update statement, but nothing actually changes.
Uniphix commentedon Apr 19, 2024
If I use pure connection.query instead of connection.execute it runs and works as expected. Even if I convert the bigint to an String value using this code ...
Neither of these update the record.
So I have to resort to using a custom query that uses replacement variables and not use any of the "bind parameters" which seems to be the problem.
I am using AWS RDS 8.0.mysql_aurora.3.04.0 which is MySQL 8.0.28 version
However after doing some research on the MySQL version changes, it seems to me that this whole parameter issue could fail to update a row is in fact an engine bug.
MySQL 8.0.31 bug related I think
I will update my staging environment to 8.0.34 (Which is the one that is highest available on AWS) and let you know if this is something we can finally close the case on.
feat: typed parameters, and adopt integer types the server reports
feat: typed parameters, and adopt integer types the server reports (#…