Skip to content

Commit 6763792

Browse files
fix(docs): Better discussion of type override nuances (#2972)
* fix(docs): Better discussion of type override nuances * Better defaults
1 parent e120ef2 commit 6763792

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

docs/howto/overrides.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Overriding types
22

3-
The default mapping of PostgreSQL/MySQL types to Go types only uses packages outside
4-
the standard library when it must.
5-
6-
For example, the `uuid` PostgreSQL type is mapped to `github.com/google/uuid`.
7-
If a different Go package for UUIDs is required, specify the package in the
8-
`overrides` array. In this case, I'm going to use the `github.com/gofrs/uuid`
3+
In many cases it's useful to tell `sqlc` explicitly what Go type you want it to
4+
use for a query input or output. For instance, a PostgreSQL UUID type will map
5+
to `UUID` from `github.com/jackc/pgx/pgtype` by default when you use
6+
`pgx/v5`, but you may want `sqlc` to use `UUID` from `github.com/google/uuid`
97
instead.
108

9+
If you'd like `sqlc` to use a different Go type, specify the package import
10+
path and type in the `overrides` list.
11+
1112
```yaml
1213
version: "2"
1314
sql:
@@ -17,34 +18,44 @@ sql:
1718
gen:
1819
go:
1920
package: "authors"
20-
out: "postgresql"
21+
out: "db"
22+
sql_package: "pgx/v5"
2123
overrides:
2224
- db_type: "uuid"
23-
go_type: "github.com/gofrs/uuid.UUID"
25+
go_type:
26+
import: "github.com/google/uuid"
27+
type: "UUID"
2428
```
2529
26-
Each mapping of the `overrides` collection has the following keys:
30+
## The `overrides` list
31+
32+
Each element in the `overrides` list has the following keys:
2733

2834
- `db_type`:
29-
- The PostgreSQL or MySQL type to override. Find the full list of supported types in [postgresql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/postgresql_type.go#L12) or [mysql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/mysql_type.go#L12). Note that for Postgres you must use the pg_catalog prefixed names where available. Can't be used if the `column` key is defined.
35+
- A database type to override. Find the full list of supported types in [postgresql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/postgresql_type.go#L12) or [mysql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/mysql_type.go#L12). Note that for Postgres you must use pg_catalog-prefixed names where available. And note that `db_type` and `column` are mutually exclusive.
3036
- `column`:
31-
- In case the type overriding should be done on specific a column of a table instead of a type. `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`. Can't be used if the `db_type` key is defined.
37+
- A column name to override. The value should be of the form `table.column` but you can also specify `schema.table.column` or `catalog.schema.table.column`. Note that `column` and `db_type` are mutually exclusive.
3238
- `go_type`:
33-
- A fully qualified name to a Go type to use in the generated code.
39+
- The fully-qualified name of a Go type to use in generated code. This is usually a string but can also be [a map](#the-go_type-map) for more complex configurations.
3440
- `go_struct_tag`:
35-
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
36-
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
41+
- A reflect-style struct tag to use in generated code, e.g. `a:"b" x:"y,z"`.
42+
If you want `json` or `db` tags for all fields, use `emit_json_tags` or `emit_db_tags` instead.
3743
- `nullable`:
38-
- If `true`, use this type when a column is nullable. Defaults to `false`.
44+
- If `true`, sqlc will apply this override when a column is nullable.
45+
Otherwise `sqlc` will apply this override when a column is non-nullable.
46+
Note that this has no effect on `column` overrides. Defaults to `false`.
3947

4048
Note that a single `db_type` override configuration applies to either nullable or non-nullable
41-
columns, but not both. If you want a single `go_type` to override in both cases, you'll
42-
need to specify two overrides.
49+
columns, but not both. If you want the same Go type to override in both cases, you'll
50+
need to configure two overrides.
51+
52+
When generating code, entries using the `column` key will always take precedence over
53+
entries using the `db_type` key.
4354

44-
When generating code, entries using the `column` key will always have preference over
45-
entries using the `db_type` key in order to generate the struct.
55+
### The `go_type` map
4656

47-
For more complicated import paths, the `go_type` can also be an object with the following keys:
57+
Some overrides may require more detailed configuration. If necessary, `go_type`
58+
can be a map with the following keys:
4859

4960
- `import`:
5061
- The import path for the package where the type is defined.
@@ -53,9 +64,9 @@ For more complicated import paths, the `go_type` can also be an object with the
5364
- `type`:
5465
- The type name itself, without any package prefix.
5566
- `pointer`:
56-
- If set to `true`, generated code will use pointers to the type rather than the type itself.
67+
- If `true`, generated code will use a pointer to the type rather than the type itself.
5768
- `slice`:
58-
- If set to `true`, generated code will use a slice of the type rather than the type itself.
69+
- If `true`, generated code will use a slice of the type rather than the type itself.
5970

6071
An example:
6172

@@ -68,12 +79,13 @@ sql:
6879
gen:
6980
go:
7081
package: "authors"
71-
out: "postgresql"
82+
out: "db"
83+
sql_package: "pgx/v5"
7284
overrides:
7385
- db_type: "uuid"
7486
go_type:
7587
import: "a/b/v2"
7688
package: "b"
7789
type: "MyType"
7890
pointer: true
79-
```
91+
```

0 commit comments

Comments
 (0)