-
Couldn't load subscription status.
- Fork 959
-
Hello!
I have a query like this playground link:
-- Example queries for sqlc CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, name text NOT NULL, bio text ); -- name: GetAuthor :one SELECT name, bio FROM authors WHERE bio IS NOT NULL;
This currently generates the following return type for the GetAuthor method:
type GetAuthorRow struct { Name string Bio sql.NullString }
Since I'm filtering for bio IS NOT NULL, is there any way to get sqlc to make Bio just be a string?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments
-
There may be other ways, but perhaps an override would work: https://docs.sqlc.dev/en/stable/reference/config.html?highlight=override#overrides
Adding the following to your sqlc.yaml might work, though you might have to play around with it a little to get what you want.
overrides:
- column: "authors.bio"
go_type: "string"
nullable: true
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm running into a use case for this as well, I had a parameter sqlc.arg('foo') that Im using in two places in my WHERE statement. In one table the column is and in the other table it's non-nullable. Sqlc throws a
error generating code: named param foo has incompatible types: int32, sql.NullInt32
To allow compilation, I have to make them both nullable through use of sqlc.narg(), which is regrettable since I don't want to support queries where this param is null.
It seems like maybe we need three functions
sqlc.arg() <- inferred type
sqlc.narg() <- force nullable
sqlc.nnarg() <- force non-nullable
Beta Was this translation helpful? Give feedback.
All reactions
-
Actually on second thought, sqlc.arg() and sqlc.narg() should be enough. In scenarios where sqlc.arg() is being used in two places (int32, and NullInt32), sqlc.arg() should just consider the arg as non-nullable, since the intersection of int32 and NullInt32 is int32.
Beta Was this translation helpful? Give feedback.