You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sqlc’s parameter nullability inference is inconsistent in many cases in the current implementation.
The expected rule appears to be:
Nullable parameters should always use nullable types
Non-null parameters should always use non-null types
sqlc.narg(...) is the explicit way to force a nullable parameter
In the docs:
"sqlc infers the nullability of any specified parameters, and often does exactly what you want. If you want finer control over the nullability of your parameters, you may use sqlc.narg()"
"for structs, null values are represented using the appropriate type from the database/sql or pgx package."
There are many correct e2e tests that handle nullability correctly but also some that don't.
We of course want parameters to match the types in structs by default. If you have pgtype.Int4 while really that parameter is NOT NULL, that's dangerous. I found multiple occurrences of this but only when sqlc.yaml included pgx. I don’t see a convincing non-pgx nullability bug in the e2e tests
In internal/endtoend/testdata/insert_select_param/postgresql/pgx/:
-- schema.sqlCREATETABLEauthors (
id BIGSERIALPRIMARY KEY,
name textNOT NULL,
bio text
);
-- query.sql-- name: InsertSelect :execINSERT INTO authors (id, name, bio)
SELECT @id, name, bio
FROM authors
WHERE name = @name;
-- query.sql-- name: InsertSelect :execINSERT INTO authors (id, name, bio)
SELECT @id, name, bio
FROM authors
WHERE name = @name;
Clearly we see that name is supposed to be NOT NULL and yet it takes on the type pgtype.Text. The same thing happens in nested_select, select_subquery, and unnest_star.
Activity
[-]Parameter nullability inference inconsistency and bugs[/-][+]pgx parameter nullability inference bugs[/+]