mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
363 stars 93 forks source link

Illegal mix of collations but.......only in some circumstances #283

Closed gartumar closed 2 months ago

gartumar commented 2 months ago

text = "UPDATE visitas SET modocobro = ? WHERE (id = ?)", values = [ "Tarjeta", 4256 ]} const result = await conn.query(text, values);

Works fine.

But this: text = "UPDATE visitas SET modocobro = ? WHERE (id = ?)", values = [ "T", 4256 ]} <-------------------------------------------------------- string length of 1 const result = await conn.query(text, values);

I got the error: SQLState: HY000) Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='\nsql: UPDATE visitas SET modocobro = ? WHERE (id = ?) - parameters:['T',4256]",

Same operation in same table with same parameters.......??????

UPDATE: The problem is MariaDB itself. I've tried running the update statement directly in mariadb and the same thing happens. modocobro="T" fails and modocobro="Ta" works correctly. modocobro is a varchar(10). I have changed the variables collation_connection, collation_database and collation_server to utf8mb4_unicode_ci but the problem persists.

UPDATE: found problem and solution

I've finally found the problem. It was actually because of a stored procedure that is called in trigger after update. I found out that originally this database was created in an Arch-like linux distribution (Manjaro) and I am now developing in Debian. The character sets and collations are different and according to Mariadb's documentation:

""The literals which occur in stored programs and views, by default, use the character set and collation which was specified by the character_set_connection and collation_connection system variables when the stored program was created. These values can be seen using the SHOW CREATE statements. To change the character sets used for literals in an existing stored program, it is necessary to drop and recreate the stored program."

I've changed the default charset in .cnf to utf8mb4=general_ci, recreated the database with collation utf8mb4_general_ci and reloaded the tables with collation utf8mb4_general_ci. Now it works.