Repository navigation
Type annotations for query parameters #2800
Description
Activity
In the named parameter example, I would expect the syntax to be
-- @name CreateAuthor :one -- @param @name TEXT NOT NULL -- @param @bio TEXTso that it's consistent with the
$nand?ntreatment. Alternatively we could change the syntax in the numbered parameter cases to remove the$and?and just leave the numbers.Reacted by Nicolás ParadaI don't like the repetition of the
@character, but agree that the?and$syntax feels unnecessary. We shouldn't allow named parameters to start with a number anyways.Reacted by Andrew Benton, Andrei Dascalu, Philip Constantinou and LTLove this. Been type casting params for awhile now, but this is a much cleaner solution imo. 👍
Does this mean we no longer would need
sqlc.narg?@Emyrk I just update the proposal with a slightly different syntax, which allows for specifying type information without change the nullability of a parameter.
Does this mean we no longer would need
sqlc.narg?Yes, we just added a bit more clarity about that to the proposal.
Reacted by Steven Masley- added a commit that references this issue
on Oct 13, 2023 - added a commit that references this issue
on Dec 21, 2023 @kyleconroy @andrewmbenton Would it be okay to use generated model structs in place of query params? By having a syntax like this:
-- @name CreateProduct :one -- @modelParam Productor any syntax that hints using model struct as params. I was able to achieve that by applyning new config option called
params_struct_overridesin my forked repo. https://github.com/aliml92/sqlc/commit/d38d58a784b40e1c871e9083cf5ae3277a33ad60. Here are the sample code:sqlc.yaml:
version: "2" sql: - engine: "postgresql" queries: "./db/queries" schema: "./db/migrations" gen: go: package: "store" sql_package: "pgx/v5" out: "./internal/store" params_struct_overrides: - method_name: CreateProduct model_name: Product
models.go
type Product struct { ID int64 `json:"id"` StoreID int32 `json:"store_id"` CategoryID int32 `json:"category_id"` Name string `json:"name"` Brand *string `json:"brand"` Slug string `json:"slug"` ImageLinks []byte `json:"image_links"` Specs []byte `json:"specs"` CreatedAt pgtype.Timestamptz `json:"created_at"` UpdatedAt pgtype.Timestamptz `json:"updated_at"` }
productcatalog.sql.go
const createProduct = `-- name: CreateProduct :one INSERT INTO products (store_id, category_id, name, brand, slug, image_links, specs) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id ` func (q *Queries) CreateProduct(ctx context.Context, p Product) (int64, error) { row := q.db.QueryRow(ctx, createProduct, p.StoreID, p.CategoryID, p.Name, p.Brand, p.Slug, p.ImageLinks, p.Specs, ) var id int64 err := row.Scan(&id) return id, err }
However, enabling such override by type annotations would be much preferable though.
Reacted by Steven Masley, Nicolás Parada, Alexander Mint and Dennis SmithHowever, enabling such override by type annotations would be much preferable though.
I also have wanted this feature, but there are potentially better ways to solve this (eg
sqlc.embedas an example).My fear of overrides is you lose some of the power of sqlc, which is making sure the models always match the query. If custom models are supported, is there anyway to add some "linting" or something that would warn the user when a new column is added and their custom model does not have it?
The query has all selected columns, so that would be possible. Just food for thought that custom structs might want some additional support to keep them "in line" with the sql.
👍 again for this. Would be really helpful alongside type overrides for some edge case stuff. Currently trying to get tuples to work as parameters
Reacted by Jacques Dafflon, Eser Ozvataf, Oleg Schwann, Mirza Hilmi and Jackie Li- added a commit that references this issue
on Oct 13, 2025 Any progress on this? I was trying to annotate types for parameters in SQLite and found #3574, and this is a great solution to that issue.
Reacted by Brett, Benjamin Kane, Alex Plescan and DanielThis would be particularly useful to let custom-defined types intended for the use together with json* functions to feel like first-class citizens in SQLITE.
Think of implementing an equivalent of Postgres'es
ANY(). I feel like usingselect value from json_each(your_custom_type_arg)is a much cleaner solution than usingsqlc.slice. Mainly because the slice doesn't always play well with other query arguments. Where's the only thing lacking for the json-based alternative is a proper type casting, so that the downstream code doesn't have to deal with any-typed query parameters.SELECT CURRENT_TIMESTAMP::timestamptz AS current_tx_timestamp;
generate
type Row struct { CurrentTxTimestamp pgtype.timestamptz }
I couldn't find a way to reload the
CurrentTxTimestampfield to thetime.Timetype.It would be great if we could overload the return field type via annotations.
I propose adding type annotations for query parameters. Users will no longer need to cast parameters to the desired type. This proposal builds on Andrew's query annotation
work for
vet.Unified syntax
We added the
@sqlc-vet-disableannotation to disable vet rules on a per-querybasis. We can extend this syntax to support other per-query configuration
options.
The current syntax for query name and command are different, so we'll
standardize on the
@prefix. This will be the new, preferred syntax for nameand command.
The existing syntax will continue to work, but it will be an error to use both
annotations on a single query.
Query command
Today, queries must have a name and a command. With the new syntax, the
command option will default to
exec.Validation
sqlc will delegate command validation to codegen plugins, allowing plugins to implement new commands without having
to merge anything into sqlc.
If you want to still validate those, you can simulate the current behavior by
using this vet rule.
Type annotations for query parameters
The
@paramannotation supports passing type information without having to usea cast.
-- @param name typeCasts are required today when sqlc infers an incorrect type, but these
casts are passed down to the engine itself, possibly hurting performance.
For example, this cast is required to get sqlc working correctly, but isn't needed at runtime.
Here's what it looks like with the new syntax. The type annotation is
engine-specific and is the same that you'd pass to CAST or CREATE TABLE.
If a parameter has a type annotation, that will be used instead of inferring the
type from the query.
NULL values
sqlc will infer the nullability of parameters by default. You can force a parameter to be nullable using the
?operator, or not null using the!operator.Positional parameters
If your parameters do not have a given name, you can refer to them by number to add a type annotation and nullability.
For PostgreSQL:
And for MySQL or SQLite:
sqlc.arg / sqlc.narg
Using the proposed annotation syntax allows you to replace
sqlc.arg()andsqlc.narg().For example this query with
sqlc.arg()andsqlc.narg()is equivalent to this one without
sqlc.argandsqlc.nargwill continue to work, but will likely be deprecated in favor of the@foosyntax.You can use
sqlc.arg()with the new@paramannotation syntax (to avoid explicit casts), but notsqlc.narg(). This constraintis intended to eliminate confusion about precedence of nullability directives.
So for example this will work
but this won't
To make this work, switch
sqlc.nargtosqlc.argand add a?to the param annotation.Why comments?
We're using comments instead of
sqlc.*functions to avoid engine-specific parsing issues. For example, we've run into issues with the MySQL parser not support functions in certain parameter locations.Full example
This is the normal example from the playground using the new syntax.
Future
The plan is to use similar annotations to support type annotations for output columns and values, Go type overrides for parameters and outputs, and JSON unmarshal / marshal hints.