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: "2"
sql:
- schema: "postgresql/schema.sql"
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
package: "authors"
out: "postgresql"
overrides:
- db_type: "uuid"
go_type: "github.com/gofrs/uuid.UUID"Each mapping of the overrides collection has the following keys:
db_type:- The PostgreSQL or MySQL type to override. Find the full list of supported types in postgresql_type.go or mysql_type.go. Note that for Postgres you must use the pg_catalog prefixed names where available. Can't be used if the
columnkey is defined.
- The PostgreSQL or MySQL type to override. Find the full list of supported types in postgresql_type.go or mysql_type.go. Note that for Postgres you must use the pg_catalog prefixed names where available. Can't be used if the
column:- In case the type overriding should be done on specific a column of a table instead of a type.
columnshould be of the formtable.columnbut you can be even more specific by specifyingschema.table.columnorcatalog.schema.table.column. Can't be used if thedb_typekey is defined.
- In case the type overriding should be done on specific a column of a table instead of a type.
go_type:- A fully qualified name to a Go type to use in the generated code.
go_struct_tag:- A reflect-style struct tag to use in the generated code, e.g.
a:"b" x:"y,z". If you want general json/db tags for all fields, useemit_db_tagsand/oremit_json_tagsinstead.
- A reflect-style struct tag to use in the generated code, e.g.
nullable:- If
true, use this type when a column is nullable. Defaults tofalse.
- If
Note that a single db_type override configuration applies to either nullable or non-nullable
columns, but not both. If you want a single go_type to override in both cases, you'll
need to specify two overrides.
When generating code, entries using the column key will always have preference over
entries using the db_type key in order to generate the struct.
For more complicated import paths, the go_type can also be an object with the following keys:
import:- The import path for the package where the type is defined.
package:- The package name where the type is defined. This should only be necessary when your import path doesn't end with the desired package name.
type:- The type name itself, without any package prefix.
pointer:- If set to
true, generated code will use pointers to the type rather than the type itself.
- If set to
slice:- If set to
true, generated code will use a slice of the type rather than the type itself.
- If set to
An example:
version: "2"
sql:
- schema: "postgresql/schema.sql"
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
package: "authors"
out: "postgresql"
overrides:
- db_type: "uuid"
go_type:
import: "a/b/v2"
package: "b"
type: "MyType"
pointer: true