2、Initial Data:
\nINSERT INTO `resource_json_test` VALUES (1, '\"dsgfdfgdg\"', '2025-03-20 20:26:00', '2025-03-20 20:28:24'); \n3、Test Code:
\nfunc TestJsonBug1(t *testing.T) { \n // Case 1: ID exists (int64) -> Fails with Error 3144 \n args1 := []any{`\"testjson\"`, `\"testjson\"`, int64(1)} \n _, err := mydb.Exec( \n `UPDATE resource_json_test SET unicast_idcs = IF(?='', NULL, ?) WHERE id = ?`, \n args1..., \n ) \n // Output: Error 3144 (22032): Cannot create a JSON value from a string with CHARACTER SET 'binary'. \n\n // Case 2: ID does not exist (int64) -> Succeeds \n args2 := []any{`\"testjson\"`, `\"testjson\"`, int64(3)} \n _, err = mydb.Exec(...) // No error \n\n // Case 3: ID exists (float64) -> Succeeds \n args3 := []any{`\"testjson\"`, `\"testjson\"`, float64(1)} \n _, err = mydb.Exec(...) // No error \n\n // Case 4: Direct assignment (no IF condition) -> Succeeds \n _, err = mydb.Exec( \n `UPDATE resource_json_test SET unicast_idcs = ? WHERE id = ?`, \n `\"testjson\"`, int64(1), \n ) // No error \n} \nObserved Behavior
\nCase 1 (int64 ID + IF condition):
\nFails with Error 3144, indicating the driver is passing the JSON string with an incorrect character set (binary).
Case 2 (Non-existent ID):
\nSucceeds because no actual JSON update occurs.
Case 3 (float64 ID):
\nSucceeds, implying the parameter type affects character set handling.
Case 4 (Direct assignment):
\nSucceeds, confirming that the issue is specific to the IF condition with parameterized logic.
Expected Behavior
\nThe parameterized IF condition should handle JSON values consistently regardless of the ID’s data type (int64 or float64).
\nAdditional Notes
\nTemporary Workaround
\nUse direct assignment (Case 4) or ensure the ID parameter is passed as float64 (not ideal).
\nPlease let me know if you need further details or logs to debug this!
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\n\n1、Why does the parameter type (int64 vs float64) affect the behavior?
\n
\nWhen using int64 for the ID, MySQL treats the second ? as a BINARY string, causing the JSON error.
\nBut with float64, the same query succeeds without CAST or interpolateParams.
I don't know. Please ask to MySQL.
\n\n\n2、Is this a driver inconsistency?
\n
No
\n\n\nShould the driver standardize parameter handling to avoid such type-dependent edge cases?
\n
No
\n\n\nOr is this a MySQL server behavior that users must explicitly mitigate (e.g., always CAST for JSON fields)?
\n
Yes, maybe. But ask to bugs.mysql.com.
\n\n\nIf this is a MySQL server limitation, adding a note to the driver’s documentation about JSON fields and placeholder inference would help users avoid similar issues.
\n
No. There are massive amount of edges in MySQL. We can not create and maintain such a note.
\n\n\nIf it’s a driver behavior, could the driver auto-detect JSON values and apply CAST internally (or warn users)?
\n
Impossible.
","upvoteCount":0,"url":"https://github.com/go-sql-driver/mysql/discussions/1685#discussioncomment-12571793"}}}-
|
Environment MySQL Version: 8.x Problem Description When executing an UPDATE statement with a parameterized IF condition on a JSON field, the driver returns Error 3144 (Cannot create a JSON value from a string with CHARACTER SET 'binary'). However, the same query works in MySQL Workbench and succeeds when:
This suggests an inconsistency in parameter handling for JSON fields under certain conditions. Steps to Reproduce 2、Initial Data: 3、Test Code: Observed Behavior Case 1 (int64 ID + IF condition): Case 2 (Non-existent ID): Case 3 (float64 ID): Case 4 (Direct assignment): Expected Behavior The parameterized IF condition should handle JSON values consistently regardless of the ID’s data type (int64 or float64). Additional Notes
Temporary Workaround Use direct assignment (Case 4) or ensure the ID parameter is passed as float64 (not ideal). Please let me know if you need further details or logs to debug this! |
Beta Was this translation helpful? Give feedback.
-
|
This driver doesn't send the string with binary collation. This driver sends There are some ways to avoid it:
|
Beta Was this translation helpful? Give feedback.
I don't know. Please ask to MySQL.
No
No
Yes, maybe. But ask to bugs.mysql.com.