Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use non-nullable columns when WHERE IS NOT NULL is used. #1779

Unanswered
tom5760 asked this question in Q&A
Discussion options

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?

You must be logged in to vote

Replies: 3 comments

Comment options

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
You must be logged in to vote
0 replies
Comment options

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
You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /