docs/reference/config.md
The sqlc tool is configured via a sqlc.(yaml|yml) or sqlc.json file. This
file must be in the directory where the sqlc command is run.
version: "2"
cloud:
project: "<PROJECT_ID>"
sql:
- schema: "postgresql/schema.sql"
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
package: "authors"
out: "postgresql"
database:
managed: true
rules:
- sqlc/db-prepare
- schema: "mysql/schema.sql"
queries: "mysql/query.sql"
engine: "mysql"
gen:
go:
package: "authors"
out: "mysql"
Each mapping in the sql collection has the following keys:
name:
engine:
postgresql, mysql or sqlite.schema:
queries:
codegen:
gen:
database:
rules:
sqlc vet. See rules for configuration options.analyzer:
strict_function_checks
false.strict_order_by
true.The codegen mapping supports the following keys:
out:
plugin:
plugins collection.options:
version: '2'
plugins:
- name: py
wasm:
url: https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm
sha256: 428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2
sql:
- schema: "schema.sql"
queries: "query.sql"
engine: postgresql
codegen:
- out: src/authors
plugin: py
options:
package: authors
emit_sync_querier: true
emit_async_querier: true
query_parameter_limit: 5
The database mapping supports the following keys:
managed:
false.uri:
The uri string can contain references to environment variables using the ${...}
syntax. In the following example, the connection string will have the value of
the PG_PASSWORD environment variable set as its password.
version: '2'
sql:
- schema: schema.sql
queries: query.sql
engine: postgresql
database:
uri: postgresql://postgres:${PG_PASSWORD}@localhost:5432/authors
gen:
go:
package: authors
out: postgresql
The analyzer mapping supports the following keys:
database:
true.The gen mapping supports the following keys:
package:
out basename.out:
sql_package:
pgx/v4, pgx/v5 or database/sql. Defaults to database/sql.sql_driver:
github.com/jackc/pgx/v4, github.com/jackc/pgx/v5, github.com/lib/pq or github.com/go-sql-driver/mysql. No defaults. Required if query annotation :copyfrom is used.emit_db_tags:
false.emit_prepared_queries:
false.emit_interface:
Querier interface in the generated package. Defaults to false.emit_exact_table_names:
false.emit_empty_slices:
:many queries will be empty instead of nil. Defaults to false.emit_exported_queries:
emit_json_tags:
false.emit_result_struct_pointers:
false.emit_params_struct_pointers:
false.emit_methods_with_db_argument:
*Queries struct. Defaults to false.emit_pointers_for_null_types:
*string) instead of database/sql null types (ie. NullString). Currently only supported for PostgreSQL if sql_package is pgx/v4 or pgx/v5, and for SQLite. Defaults to false. Nullable enum columns also follow this setting unless emit_pointers_for_null_enum_types is set.emit_pointers_for_null_enum_types:
emit_pointers_for_null_types for nullable enum columns only. When true, nullable enum columns are emitted as pointers (ie. *UserRole). When false, nullable enum columns use the generated NullUserRole wrapper struct even if emit_pointers_for_null_types is true. Set this to false to keep the pre-v1.31 behavior when upgrading. Only applies to PostgreSQL with sql_package pgx/v4 or pgx/v5.emit_enum_valid_method:
emit_all_enum_values:
emit_sql_as_comment:
false.build_tags:
//go:build <build_tags> directive at the beginning of each generated Go file.initialisms:
app_id becomes AppID. Defaults to ["id"].json_tags_id_uppercase:
falsejson_tags_case_style:
camel for camelCase, pascal for PascalCase, snake for snake_case or none to use the column name in the DB. Defaults to none.omit_unused_structs:
true, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to false.output_batch_file_name:
batch.go.output_db_file_name:
db.go.output_models_file_name:
models.go.output_querier_file_name:
querier.go.output_copyfrom_file_name:
copyfrom.go.output_files_suffix:
query_parameter_limit:
0. Defaults to 1.rename:
overrides:
See Overriding types for an in-depth guide to using type overrides.
Removed in v1.17.0 and replaced by the sqlc-gen-kotlin plugin. Follow the migration guide to switch.
package:
out:
emit_exact_table_names:
false.Removed in v1.17.0 and replaced by the sqlc-gen-python plugin. Follow the migration guide to switch.
package:
out:
emit_exact_table_names:
false.emit_sync_querier:
false.emit_async_querier:
false.emit_pydantic_models:
pydantic.BaseModel. Otherwise, define classes using the dataclass decorator. Defaults to false.out:
filename:
codegen_request.json.indent:
.Each mapping in the plugins collection has the following keys:
name:
env
process: A mapping with a single cmd key
cmd:
format:
json and protobuf formats. Defaults to protobuf.wasm: A mapping with a two keys url and sha256
url:
https:// or file:// schemes.sha256
version: "2"
plugins:
- name: "py"
wasm:
url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm"
sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2"
- name: "js"
env:
- PATH
process:
cmd: "sqlc-gen-json"
Each mapping in the rules collection has the following keys:
name:
rule:
message:
true.See the vet documentation for a list of built-in rules and help writing custom rules.
version: "2"
sql:
- schema: "query.sql"
queries: "query.sql"
engine: "postgresql"
gen:
go:
package: "authors"
out: "db"
rules:
- no-pg
- no-delete
- only-one-param
- no-exec
rules:
- name: no-pg
message: "invalid engine: postgresql"
rule: |
config.engine == "postgresql"
- name: no-delete
message: "don't use delete statements"
rule: |
query.sql.contains("DELETE")
- name: only-one-param
message: "too many parameters"
rule: |
query.params.size() > 1
- name: no-exec
message: "don't use exec"
rule: |
query.cmd == "exec"
Sometimes, the same configuration must be done across various specifications of
code generation. Then a global definition for type overriding and field
renaming can be done using the overrides mapping the following manner:
version: "2"
overrides:
go:
rename:
id: "Identifier"
overrides:
- db_type: "pg_catalog.timestamptz"
nullable: true
engine: "postgresql"
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Time"
sql:
- schema: "postgresql/schema.sql"
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
package: "authors"
out: "postgresql"
- schema: "mysql/schema.sql"
queries: "mysql/query.sql"
engine: "mysql"
gen:
go:
package: "authors"
out: "mysql"
With the previous configuration, whenever a struct field is generated from a
table column that is called id, it will generated as Identifier.
Also, whenever there is a nullable timestamp with time zone column in a
Postgres table, it will be generated as null.Time. Note that the mapping for
global type overrides has a field called engine that is absent in the regular
type overrides. This field is only used when there are multiple definitions
using multiple engines. Otherwise, the value of the engine key
defaults to the engine that is currently being used.
Currently, type overrides and field renaming, both global and regular, are only fully supported in Go.
version: "1"
packages:
- name: "db"
path: "internal/db"
queries: "./sql/query/"
schema: "./sql/schema/"
engine: "postgresql"
emit_db_tags: false
emit_prepared_queries: true
emit_interface: false
emit_exact_table_names: false
emit_empty_slices: false
emit_exported_queries: false
emit_json_tags: true
emit_result_struct_pointers: false
emit_params_struct_pointers: false
emit_methods_with_db_argument: false
emit_pointers_for_null_types: false
emit_enum_valid_method: false
emit_all_enum_values: false
build_tags: "some_tag"
json_tags_case_style: "camel"
omit_unused_structs: false
output_batch_file_name: "batch.go"
output_db_file_name: "db.go"
output_models_file_name: "models.go"
output_querier_file_name: "querier.go"
output_copyfrom_file_name: "copyfrom.go"
query_parameter_limit: 1
Each mapping in the packages collection has the following keys:
name:
path basename.path:
queries:
schema:
engine:
postgresql or mysql. Defaults to postgresql.sql_package:
pgx/v4, pgx/v5 or database/sql. Defaults to database/sql.overrides:
emit_db_tags:
false.emit_prepared_queries:
false.emit_interface:
Querier interface in the generated package. Defaults to false.emit_exact_table_names:
false.emit_empty_slices:
:many queries will be empty instead of nil. Defaults to false.emit_exported_queries:
emit_json_tags:
false.emit_result_struct_pointers:
false.emit_params_struct_pointers:
false.emit_methods_with_db_argument:
*Queries struct. Defaults to false.emit_pointers_for_null_types:
sql_package is set to pgx/v4 or pgx/v5, generated types for nullable columns are emitted as pointers (ie. *string) instead of database/sql null types (ie. NullString). Defaults to false. Nullable enum columns also follow this setting unless emit_pointers_for_null_enum_types is set.emit_pointers_for_null_enum_types:
emit_pointers_for_null_types for nullable enum columns only. When true, nullable enum columns are emitted as pointers (ie. *UserRole). When false, nullable enum columns use the generated NullUserRole wrapper struct even if emit_pointers_for_null_types is true. Set this to false to keep the pre-v1.31 behavior when upgrading. Only applies to PostgreSQL with sql_package pgx/v4 or pgx/v5.emit_enum_valid_method:
emit_all_enum_values:
build_tags:
//go:build <build_tags> directive at the beginning of each generated Go file.json_tags_case_style:
camel for camelCase, pascal for PascalCase, snake for snake_case or none to use the column name in the DB. Defaults to none.omit_unused_structs:
true, sqlc won't generate table and enum structs that aren't used in queries for a given package. Defaults to false.output_batch_file_name:
batch.go.output_db_file_name:
db.go.output_models_file_name:
models.go.output_querier_file_name:
querier.go.output_copyfrom_file_name:
copyfrom.go.output_files_suffix:
query_parameter_limit:
>= 0). To always emit a parameter struct, you would need to set it to 0. Defaults to 1.See the version 1 configuration section of the Overriding types guide for details.
Struct field names are generated from column names using a simple algorithm: split the column name on underscores and capitalize the first letter of each part.
account -> Account
spotify_url -> SpotifyUrl
app_id -> AppID
If you're not happy with a field's generated name, use the rename mapping
to pick a new name. The keys are column names and the values are the struct
field name to use.
version: "1"
packages: [...]
rename:
spotify_url: "SpotifyURL"