-
Notifications
You must be signed in to change notification settings - Fork 885
Paramaters matched to JSON fields are the wrong type #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @kyleconroy, I have the same problem as in issue #738, I have table like this: CREATE TABLE "user" (
"id" INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
"metadata" JSONB
) but instead of having an object, I have an array of users:
then I wrote a query to get a user by "lic" -- name: FindUserByLic :one
SELECT * FROM user AS u WHERE u.deleted_at IS NULL AND u.metadata @> $1; -- > $1 suppose to be like this '[{"lic":"1295364R"}]' and I got this parameter: type FindUserByLic struct {
metadata json.RawMessage
} here is my code: lic, err := json.Marshal(fmt.Sprintf(`'[{"lic":"%s"}]'`, req.Lic))
if err != nil {
return err
}
arg := db.FindUserByLicParams{
Metadata: lic,
}
contests, err := server.store.FindUserByLic(ctx, arg) //find user by lic
if err != nil {
return err
} but in the end I had an empty array :/ |
That is not a bug, I have a solution for that or any parametrized representation of jsonb operations, the Postgres way. First you need to define the datatype of your parameter on your sqlc definitions, for example:
After generating the code, you would expect the input to be of type pgtype.JSONB. Then in your go code, you would have to have an interface conversion to JSONB:
Last, running your query, you have this:
And works as you would normally write your query in the console. |
This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future. You can play around with the working example on the playground |
Uh oh!
There was an error while loading. Please reload this page.
These parameters should end up as
interface{}
, notjson.RawMessage
.https://play.sqlc.dev/p/cd6227106fb8fb969d1fc9699dd9c03046e26578c589268e30d23573f67037bb
The text was updated successfully, but these errors were encountered: