Skip to content

If a BigInt/Int field exists on update, the record is not updated. #1483

Open

Description

@microvn

It seems that this problem is related to the original mysql2. I think it's really important. 🤦🏼‍♂️

sequelize/sequelize#13724

Activity

  1. CarelessInternet commented on Jan 7, 2022

    @CarelessInternet

    Same problem here, can't update the field int value where the other field value is of bigint type

  2. sidorares commented on Jan 13, 2022

    @sidorares
    Owner

    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 )

  3. CarelessInternet commented on Jan 20, 2022

    @CarelessInternet

    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

    CREATE DATABASE IF NOT EXISTS test
    USE test;
    
    CREATE TABLE IF NOT EXISTS Example (
      IntField int,
      BigIntField bigint(18)
    );
    
    INSERT INTO Example (IntField, BigIntField) VALUES (123, 8666079548394975); -- 16 digits
    INSERT INTO Example (IntField, BigIntField) VALUES (1337, 86660795483949752); -- 17 digits

    index.js

    const { createConnection } = require('mysql2/promise');
    
    (async () => {
    	try {
    		const conn = await createConnection({
    			host: 'localhost',
    			user: 'root',
    			password: 'root',
    			database: 'test',
    			supportBigNumbers: true,
    			bigNumberStrings: true
    		});
    
    		await conn.execute(
    			'UPDATE Example SET IntField = ? WHERE BigIntField = ?',
    			[5, 8666079548394975n]
    		);
    		const [first] = await conn.execute(
    			'SELECT * FROM Example WHERE BigIntField = ?',
    			[8666079548394975n]
    		);
    
    		console.log('test #1', first[0].IntField === 5, first); // supposed to return true and it does
    
    		await conn.execute(
    			'UPDATE Example SET IntField = ? WHERE BigIntField = ?',
    			[10, 86660795483949752n]
    		);
    		const [second] = await conn.execute(
    			'SELECT * FROM Example WHERE BigIntField = ?',
    			[86660795483949752n]
    		);
    
    		console.log('test #2', second[0].IntField === 10, second); // supposed to return true but it doesn't
    
    		await conn.execute(
    			'UPDATE Example SET IntField = ? WHERE BigIntField = ?',
    			['15', '86660795483949752']
    		);
    		const [third] = await conn.execute(
    			'SELECT * FROM Example WHERE BigIntField = ?',
    			['86660795483949752']
    		);
    
    		console.log('test #3', third[0].IntField === 15, third); // supposed to return true but it doesn't
    
    		await conn.execute(
    			'UPDATE Example SET IntField = ? WHERE BigIntField = ?',
    			[20, '8666079548394975']
    		);
    		const [fourth] = await conn.execute(
    			'SELECT * FROM Example WHERE BigIntField = ?',
    			['8666079548394975']
    		);
    
    		console.log('test #4', fourth[0].IntField === 20, fourth); // supposed to return true and it does
    
    		await conn.end();
    		process.exit(0);
    	} catch (err) {
    		console.error(err);
    		process.exit(1);
    	}
    })();
  4. microvn commented on Feb 9, 2022

    @microvn
    Author

    up.

  5. CarelessInternet commented on Mar 30, 2022

    @CarelessInternet

    @sidorares any updates?

  6. sidorares commented on Mar 31, 2022

    @sidorares
    Owner

    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 UPDATE

    in a table with BigIntField: '86660795483949752' bigint(18) row doing

    const [row] = await conn.execute('SELECT * from Example WHERE BigIntField = ?', [86660795483949752n]);

    yields 1 row as expected, while

    await conn.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 ?

  7. sidorares commented on Mar 31, 2022

    @sidorares
    Owner

    hm, what's even more strange:

    // this updates 0 columns
    await conn.execute('UPDATE Example SET IntField = ? WHERE BigIntField = ?', [10, 86660795483949752n]);
    
    // this does update one expected column
    await conn.execute('UPDATE Example SET IntField = 10 WHERE BigIntField = ?', [86660795483949752n]);
  8. added
    high-priority
    on May 11, 2022
  9. Uniphix commented on Jul 27, 2022

    @Uniphix

    I have this same issue right now and it is actually hindering my project with work because of it.

  10. Uniphix commented on Jul 27, 2022

    @Uniphix

    Is there a current work around with this?

  11. CarelessInternet commented on Jul 28, 2022

    @CarelessInternet

    Is there a current work around with this?

    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.

  12. microvn commented on Jul 29, 2022

    @microvn
    Author

    The problem is still unresolved. I am not knowledgeable enough to improve this project

  13. ruiquelhas commented on Jul 29, 2022

    @ruiquelhas
    Contributor

    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 UPDATE

    in a table with BigIntField: '86660795483949752' bigint(18) row doing

    const [row] = await conn.execute('SELECT * from Example WHERE BigIntField = ?', [86660795483949752n]);

    yields 1 row as expected, while

    await conn.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).

  14. sidorares commented on Jul 29, 2022

    @sidorares
    Owner

    thanks @ruiquelhas

    I suspect #1483 (comment) can be replicated in any client and is a feature/bug of a mysql server

  15. ruiquelhas commented on Jul 29, 2022

    @ruiquelhas
    Contributor

    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.

  16. sidorares commented on Jul 29, 2022

    @sidorares
    Owner

    @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

  17. ruiquelhas commented on Jul 30, 2022

    @ruiquelhas
    Contributor

    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.

  18. Uniphix commented on Aug 5, 2022

    @Uniphix

    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.

  19. Uniphix commented on Sep 15, 2022

    @Uniphix

    Just curious on how this fix progressing?

  20. Uniphix commented on Aug 7, 2023

    @Uniphix

    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.

  21. hoangmf commented on Aug 8, 2023

    @hoangmf

    @Uniphix I think it was abandoned. try wrapping with string. Hope it helps you

  22. Uniphix commented on Nov 8, 2023

    @Uniphix

    This still doesn't work, I wrapped it as a string and it will not update. The LOG shows the Update Statement example:

    UPDATE `address` SET `line2`=?,`lat`=?,`long`=? WHERE `id` = ?; "123", 36.5854828, -82.3511855, "1569735910751208448"

    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.

  23. Uniphix commented on Mar 18, 2024

    @Uniphix

    This still will not work with 3.9.2. It calls the update statement, but nothing actually changes.

  24. Uniphix commented on Apr 19, 2024

    @Uniphix

    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 ...

    await conn.execute('UPDATE `address` SET `line2`=?,`lat`=?,`long`=? WHERE `id` = ?', ["123", 36.5854828, -82.3511855, "1569735910751208448"]);
    await conn.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.

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

    high-priorityneeds investigation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions