Named parameters for MySQL inserts / updates #3038
-
What do you want to change?I'm trying to generate code based on queries like this
So that I can use the NamedExec function of the mysql driver. But whenever I run sqlc generate I get this error
Is this not supported at all? Or am I doing it wrong? Thank you! What database engines need to be changed?MySQL What programming language backends need to be changed?Go |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
MySQL does not support NamedParameter |
Beta Was this translation helpful? Give feedback.
-
Hello, do you mean it's not supported in sqlc? Because MySQL itself does support named parameters just fine as far as my testing goes. I used the .NamedExec of the sql driver in go, and got the expected results |
Beta Was this translation helpful? Give feedback.
-
The named parameter syntax you're using is not native to MySQL. It's an extension added by sqlx. To use named parameters with sqlc, you need to use the /* name: InsertPurchaseGuid :exec */
INSERT INTO purchase_guids (
external_id,
guid,
created_at,
updated_at,
partner_id,
payment_method_id
) VALUES (
sqlc.arg('external_id'),
sqlc.arg('guid'),
sqlc.arg('created_at'),
sqlc.arg('updated_at'),
sqlc.arg('partner_id'),
sqlc.arg('payment_method_id')
); |
Beta Was this translation helpful? Give feedback.
The named parameter syntax you're using is not native to MySQL. It's an extension added by sqlx. To use named parameters with sqlc, you need to use the
sqlc.arg
function, like so: