Skip to content
\n

2、​Initial Data:

\n
INSERT INTO `resource_json_test` VALUES (1, '\"dsgfdfgdg\"', '2025-03-20 20:26:00', '2025-03-20 20:28:24');  \n
\n

3、​Test Code:

\n
func 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}  \n
\n

Observed Behavior

\n

​Case 1 (int64 ID + IF condition):
\nFails with Error 3144, indicating the driver is passing the JSON string with an incorrect character set (binary).

\n

​Case 2 (Non-existent ID):
\nSucceeds because no actual JSON update occurs.

\n

​Case 3 (float64 ID):
\nSucceeds, implying the parameter type affects character set handling.

\n

​Case 4 (Direct assignment):
\nSucceeds, confirming that the issue is specific to the IF condition with parameterized logic.

\n

Expected Behavior

\n

The parameterized IF condition should handle JSON values consistently regardless of the ID’s data type (int64 or float64).

\n

Additional Notes

\n\n

Temporary Workaround

\n

Use direct assignment (Case 4) or ensure the ID parameter is passed as float64 (not ideal).

\n

Please let me know if you need further details or logs to debug this!

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"
\n

​1、Why does the parameter type (int64 vs float64) affect the behavior?
\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.

\n
\n

I don't know. Please ask to MySQL.

\n
\n

​2、Is this a driver inconsistency?

\n
\n

No

\n
\n

Should the driver standardize parameter handling to avoid such type-dependent edge cases?

\n
\n

No

\n
\n

Or is this a MySQL server behavior that users must explicitly mitigate (e.g., always CAST for JSON fields)?

\n
\n

Yes, maybe. But ask to bugs.mysql.com.

\n
\n

If 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
\n

No. There are massive amount of edges in MySQL. We can not create and maintain such a note.

\n
\n

If it’s a driver behavior, could the driver auto-detect JSON values and apply CAST internally (or warn users)?

\n
\n

Impossible.

","upvoteCount":0,"url":"https://github.com/go-sql-driver/mysql/discussions/1685#discussioncomment-12571793"}}}
Discussion options

You must be logged in to vote

​1、Why does the parameter type (int64 vs float64) affect the behavior?
When using int64 for the ID, MySQL treats the second ? as a BINARY string, causing the JSON error.
But with float64, the same query succeeds ​without CAST or interpolateParams.

I don't know. Please ask to MySQL.

​2、Is this a driver inconsistency?

No

Should the driver standardize parameter handling to avoid such type-dependent edge cases?

No

Or is this a MySQL server behavior that users must explicitly mitigate (e.g., always CAST for JSON fields)?

Yes, maybe. But ask to bugs.mysql.com.

If this is a MySQL server limitation, adding a note to the driver’s documentation about JSON fields and placeholder inference w…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@suiriass
Comment options

@methane
Comment options

Answer selected by suiriass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1684 on March 21, 2025 00:01.