The sqlc
tool is configured via a sqlc.yaml
or sqlc.json
file. This file must be
in the directory where the sqlc
command is run.
version: "1"
packages:
- name: "db"
path: "internal/db"
queries: "./sql/query/"
schema: "./sql/schema/"
engine: "postgresql"
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
json_tags_case_style: "camel"
output_db_file_name: "db.go"
output_models_file_name: "models.go"
output_querier_file_name: "querier.go"
Each package document has the following keys:
name
:
path
basenamepath
:
queries
:
schema
:
engine
:
postgresql
or mysql
. Defaults to postgresql
.sql_package
:
pgx/v4
or database/sql
. Defaults to database/sql
.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
.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
.output_db_file_name
:
db.go
.output_models_file_name
:
models.go
.output_querier_file_name
:
querier.go
.output_files_suffix
:
The default mapping of PostgreSQL/MySQL types to Go types only uses packages outside the standard library when it must.
For example, the uuid
PostgreSQL type is mapped to github.com/google/uuid
.
If a different Go package for UUIDs is required, specify the package in the
overrides
array. In this case, I'm going to use the github.com/gofrs/uuid
instead.
version: "1"
packages: [...]
overrides:
- go_type: "github.com/gofrs/uuid.UUID"
db_type: "uuid"
Each override document has the following keys:
db_type
:
go_type
:
nullable
:
false
.For more complicated import paths, the go_type
can also be an object.
version: "1"
packages: [...]
overrides:
- db_type: "uuid"
go_type:
- import: "a/b/v2"
package: "b"
type: "MyType"
pointer: false # or true
Sometimes you would like to override the Go type used in model or query generation for a specific field of a table and not on a type basis as described in the previous section.
This may be configured by specifying the column
property in the override definition. column
should be of the form table.column
but you can be even more specific by specifying schema.table.column
or catalog.schema.table.column
.
version: "1"
packages: [...]
overrides:
- column: "authors.id"
go_type: "github.com/segmentio/ksuid.KSUID"
Overrides can be configured globally, as demonstrated in the previous sections, or they can be configured on a per-package which scopes the override behavior to just a single package:
version: "1"
packages:
- overrides: [...]
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
dictionary
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"