Skip to content

Commit 1e0ca92

Browse files
docs: clean up and add to docs regarding type overrides (#4060)
1 parent 6ae5bcc commit 1e0ca92

File tree

3 files changed

+120
-123
lines changed

3 files changed

+120
-123
lines changed

docs/howto/overrides.md

Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# Overriding types
22

3+
.. note:: Type overrides and field renaming are only fully-supported for Go.
4+
35
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`
7-
instead.
6+
use for a query input or output. For instance, by default when you use
7+
`pgx/v5`, `sqlc` will map a PostgreSQL UUID type to `UUID` from `github.com/jackc/pgx/pgtype`.
8+
But you may want `sqlc` to use `UUID` from `github.com/google/uuid` instead.
9+
10+
To tell `sqlc` to use a different Go type, add an entry to the `overrides` list in your
11+
configuration.
812

9-
If you'd like `sqlc` to use a different Go type, specify the package import
10-
path and type in the `overrides` list.
13+
`sqlc` offers two kinds of Go type overrides:
14+
* `db_type` overrides, which override the Go type for a specific database type.
15+
* `column` overrides, which override the Go type for a column or columns by name.
16+
17+
Here's an example including one of each kind:
1118

1219
```yaml
1320
version: "2"
@@ -22,11 +29,19 @@ sql:
2229
sql_package: "pgx/v5"
2330
overrides:
2431
- db_type: "uuid"
32+
nullable: true
2533
go_type:
2634
import: "github.com/google/uuid"
2735
type: "UUID"
36+
- column: "users.birthday"
37+
go_type: "time.Time"
2838
```
2939
40+
.. tip::
41+
A single `db_type` override configuration applies to either nullable or non-nullable
42+
columns, but not both. If you want the same Go type to override regardless of
43+
nullability, you'll need to configure two overrides: one with `nullable: true` and one without.
44+
3045
## The `overrides` list
3146

3247
Each element in the `overrides` list has the following keys:
@@ -39,21 +54,23 @@ Each element in the `overrides` list has the following keys:
3954
- 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.
4055
- `go_struct_tag`:
4156
- 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.
57+
If you want `json` or `db` tags for all fields, configure `emit_json_tags` or `emit_db_tags` instead.
4358
- `unsigned`:
44-
- If `true`, sqlc will apply this override when a numeric db_type is unsigned.
45-
Note that this has no effect on `column` overrides. Defaults to `false`.
59+
- If `true`, sqlc will apply this override when a numeric column is unsigned.
60+
Note that this only applies to `db_type` overrides and has no effect on `column` overrides.
61+
Defaults to `false`.
4662
- `nullable`:
4763
- If `true`, sqlc will apply this override when a column is nullable.
4864
Otherwise `sqlc` will apply this override when a column is non-nullable.
49-
Note that this has no effect on `column` overrides. Defaults to `false`.
65+
Note that this only applies to `db_type` overrides and has no effect on `column` overrides.
66+
Defaults to `false`.
5067

51-
Note that a single `db_type` override configuration applies to either nullable or non-nullable
52-
columns, but not both. If you want the same Go type to override in both cases, you'll
53-
need to configure two overrides.
68+
.. tip::
69+
A single `db_type` override configuration applies to either nullable or non-nullable
70+
columns, but not both. If you want the same Go type to override regardless of nullability, you'll
71+
need to configure two overrides: one with `nullable: true` and one without.
5472

55-
When generating code, entries using the `column` key will always take precedence over
56-
entries using the `db_type` key.
73+
.. note:: When generating code, `column` override configurations take precedence over `db_type` configurations.
5774

5875
### The `go_type` map
5976

@@ -80,7 +97,7 @@ sql:
8097
queries: "postgresql/query.sql"
8198
engine: "postgresql"
8299
gen:
83-
go:
100+
go:
84101
package: "authors"
85102
out: "db"
86103
sql_package: "pgx/v5"
@@ -92,3 +109,74 @@ sql:
92109
type: "MyType"
93110
pointer: true
94111
```
112+
113+
## Global overrides
114+
115+
To override types in all packages that `sqlc` generates, add an override
116+
configuration to the top-level `overrides` section of your `sqlc` config:
117+
118+
```yaml
119+
version: "2"
120+
overrides:
121+
go:
122+
overrides:
123+
- db_type: "pg_catalog.timestamptz"
124+
nullable: true
125+
engine: "postgresql"
126+
go_type:
127+
import: "gopkg.in/guregu/null.v4"
128+
package: "null"
129+
type: "Time"
130+
sql:
131+
- schema: "service1/schema.sql"
132+
queries: "service1/query.sql"
133+
engine: "postgresql"
134+
gen:
135+
go:
136+
package: "service1"
137+
out: "service1"
138+
- schema: "service2/schema.sql"
139+
queries: "service2/query.sql"
140+
engine: "postgresql"
141+
gen:
142+
go:
143+
package: "service2"
144+
out: "service2"
145+
```
146+
147+
Using this configuration, whenever there is a nullable `timestamp with time zone`
148+
column in a Postgres table, `sqlc` will generate Go code using `null.Time`.
149+
150+
Note that the mapping for global type overrides has a field called `engine` that
151+
is absent in per-package type overrides. This field is only used when there are
152+
multiple `sql` sections using different engines. If you're only generating code
153+
for a single database engine you can omit it.
154+
155+
#### Version 1 configuration
156+
157+
If you are using the older version 1 of the `sqlc` configuration format, override
158+
configurations themselves are unchanged but are nested differently.
159+
160+
Per-package configurations are nested under the `overrides` key within an item
161+
in the `packages` list:
162+
163+
```yaml
164+
version: "1"
165+
packages:
166+
- name: "db"
167+
path: "internal/db"
168+
queries: "./sql/query/"
169+
schema: "./sql/schema/"
170+
engine: "postgresql"
171+
overrides: [...]
172+
```
173+
174+
And global configurations are nested under the top-level `overrides` key:
175+
176+
```yaml
177+
version: "1"
178+
packages: [...]
179+
overrides:
180+
- db_type: "uuid"
181+
go_type: "github.com/gofrs/uuid.UUID"
182+
```

docs/reference/config.md

Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -194,36 +194,11 @@ The `gen` mapping supports the following keys:
194194
- `rename`:
195195
- Customize the name of generated struct fields. See [Renaming fields](../howto/rename.md) for usage information.
196196
- `overrides`:
197-
- It is a collection of definitions that dictates which types are used to map a database types.
197+
- A collection of configurations to override sqlc's default Go type choices. See [Overriding types](../howto/overrides.md) for usage information.
198198

199199
##### overrides
200200

201-
See [Overriding types](../howto/overrides.md) for an in-depth guide to using type overrides. Each mapping of the `overrides` collection has the following keys:
202-
203-
- `db_type`:
204-
- 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.
205-
- `column`:
206-
- 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.
207-
- `go_type`:
208-
- A fully qualified name to a Go type to use in the generated code.
209-
- `go_struct_tag`:
210-
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
211-
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
212-
- `nullable`:
213-
- If `true`, use this type when a column is nullable. Defaults to `false`.
214-
215-
For more complicated import paths, the `go_type` can also be an object with the following keys:
216-
217-
- `import`:
218-
- The import path for the package where the type is defined.
219-
- `package`:
220-
- 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.
221-
- `type`:
222-
- The type name itself, without any package prefix.
223-
- `pointer`:
224-
- If set to `true`, generated code will use pointers to the type rather than the type itself.
225-
- `slice`:
226-
- If set to `true`, generated code will use a slice of the type rather than the type itself.
201+
See [Overriding types](../howto/overrides.md) for an in-depth guide to using type overrides.
227202

228203
#### kotlin
229204

@@ -356,7 +331,7 @@ overrides:
356331
rename:
357332
id: "Identifier"
358333
overrides:
359-
- db_type: "timestamptz"
334+
- db_type: "pg_catalog.timestamptz"
360335
nullable: true
361336
engine: "postgresql"
362337
go_type:
@@ -368,7 +343,7 @@ sql:
368343
queries: "postgresql/query.sql"
369344
engine: "postgresql"
370345
gen:
371-
go:
346+
go:
372347
package: "authors"
373348
out: "postgresql"
374349
- schema: "mysql/schema.sql"
@@ -443,6 +418,8 @@ Each mapping in the `packages` collection has the following keys:
443418
- Either `postgresql` or `mysql`. Defaults to `postgresql`.
444419
- `sql_package`:
445420
- Either `pgx/v4`, `pgx/v5` or `database/sql`. Defaults to `database/sql`.
421+
- `overrides`:
422+
- A list of type override configurations. See the [Overriding types](../howto/overrides.md) guide for details.
446423
- `emit_db_tags`:
447424
- If true, add DB tags to generated structs. Defaults to `false`.
448425
- `emit_prepared_queries`:
@@ -494,78 +471,7 @@ Each mapping in the `packages` collection has the following keys:
494471

495472
### overrides
496473

497-
The default mapping of PostgreSQL/MySQL types to Go types only uses packages outside
498-
the standard library when it must.
499-
500-
For example, the `uuid` PostgreSQL type is mapped to `github.com/google/uuid`.
501-
If a different Go package for UUIDs is required, specify the package in the
502-
`overrides` array. In this case, I'm going to use the `github.com/gofrs/uuid`
503-
instead.
504-
505-
```yaml
506-
version: "1"
507-
packages: [...]
508-
overrides:
509-
- go_type: "github.com/gofrs/uuid.UUID"
510-
db_type: "uuid"
511-
```
512-
513-
Each override document has the following keys:
514-
515-
- `db_type`:
516-
- 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.
517-
- `go_type`:
518-
- A fully qualified name to a Go type to use in the generated code.
519-
- `go_struct_tag`:
520-
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
521-
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
522-
- `nullable`:
523-
- If true, use this type when a column is nullable. Defaults to `false`.
524-
525-
Note that a single `db_type` override configuration applies to either nullable or non-nullable
526-
columns, but not both. If you want a single `go_type` to override in both cases, you'll
527-
need to specify two overrides.
528-
529-
For more complicated import paths, the `go_type` can also be an object.
530-
531-
```yaml
532-
version: "1"
533-
packages: [...]
534-
overrides:
535-
- db_type: "uuid"
536-
go_type:
537-
import: "a/b/v2"
538-
package: "b"
539-
type: "MyType"
540-
```
541-
542-
#### Per-Column Type Overrides
543-
544-
Sometimes you would like to override the Go type used in model or query generation for
545-
a specific field of a table and not on a type basis as described in the previous section.
546-
547-
This may be configured by specifying the `column` property in the override definition. `column`
548-
should be of the form `table.column` but you can be even more specific by specifying `schema.table.column`
549-
or `catalog.schema.table.column`.
550-
551-
```yaml
552-
version: "1"
553-
packages: [...]
554-
overrides:
555-
- column: "authors.id"
556-
go_type: "github.com/segmentio/ksuid.KSUID"
557-
```
558-
559-
#### Package Level Overrides
560-
561-
Overrides can be configured globally, as demonstrated in the previous sections, or they can be configured per-package which
562-
scopes the override behavior to just a single package:
563-
564-
```yaml
565-
version: "1"
566-
packages:
567-
- overrides: [...]
568-
```
474+
See the version 1 configuration section of the [Overriding types](../howto/overrides.md#version-1-configuration) guide for details.
569475

570476
### rename
571477

docs/reference/datatypes.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
database types to Go types. Choices for more complex types are described below.
55

66
If you're unsatisfied with the default, you can override any type using the
7-
[overrides list](config.md#type-overriding) in your `sqlc` config file.
7+
[overrides list](config.md#overrides) in your `sqlc` config file.
88

99
## Arrays
1010

@@ -143,7 +143,9 @@ type Author struct {
143143
}
144144
```
145145

146-
For MySQL, there is no native `uuid` data type. When using `UUID_TO_BIN` to store a `UUID()`, the underlying field type is `BINARY(16)` which by default sqlc would interpret this to `sql.NullString`. To have sqlc automatically convert these fields to a `uuid.UUID` type, use an overide on the column storing the `uuid`.
146+
For MySQL, there is no native `uuid` data type. When using `UUID_TO_BIN` to store a `UUID()`, the underlying field type is `BINARY(16)` which by default sqlc would map to `sql.NullString`. To have sqlc automatically convert these fields to a `uuid.UUID` type, use an overide on the column storing the `uuid`
147+
(see [Overriding types](../howto/overrides.md) for details).
148+
147149
```json
148150
{
149151
"overrides": [
@@ -158,7 +160,8 @@ For MySQL, there is no native `uuid` data type. When using `UUID_TO_BIN` to stor
158160
## JSON
159161

160162
By default, sqlc will generate the `[]byte`, `pgtype.JSON` or `json.RawMessage` for JSON column type.
161-
But if you use the `pgx/v5` sql package then you can specify a struct instead of default type.
163+
But if you use the `pgx/v5` sql package then you can specify a struct instead of the default type
164+
(see [Overriding types](../howto/overrides.md) for details).
162165
The `pgx` implementation will marshal/unmarshal the struct automatically.
163166

164167
```go
@@ -209,7 +212,7 @@ type Book struct {
209212

210213
In PostgreSQL, when you have a column with the TEXT type, sqlc will map it to a Go string by default. This default mapping applies to `TEXT` columns that are not nullable. However, for nullable `TEXT` columns, sqlc maps them to `pgtype.Text` when using the pgx/v5 driver. This distinction is crucial for developers looking to handle null values appropriately in their Go applications.
211214

212-
To accommodate nullable strings and map them to `*string` in Go, you can use the `emit_pointers_for_null_types` option in your sqlc configuration. This option ensures that nullable SQL columns are represented as pointer types in Go, allowing for a clear distinction between null and non-null values. Another way to do this is by passing the option `pointer: true` when you are overriding the `TEXT` datatype in you sqlc config file.
215+
To accommodate nullable strings and map them to `*string` in Go, you can use the `emit_pointers_for_null_types` option in your sqlc configuration. This option ensures that nullable SQL columns are represented as pointer types in Go, allowing for a clear distinction between null and non-null values. Another way to do this is by passing the option `pointer: true` when you are overriding the `TEXT` datatype in your sqlc config file (see [Overriding types](../howto/overrides.md) for details).
213216

214217
## Geometry
215218

@@ -222,7 +225,7 @@ package for working with PostGIS geometry types in [GEOS](https://libgeos.org/).
222225

223226
There are three steps:
224227

225-
1. Configure sqlc to use `*github.com/twpayne/go-geos.Geom` for geometry types.
228+
1. Configure sqlc to use `*github.com/twpayne/go-geos.Geom` for geometry types (see [Overriding types](../howto/overrides.md) for details).
226229
2. Call `github.com/twpayne/pgx-geos.Register` on each
227230
`*github.com/jackc/pgx/v5.Conn`.
228231
3. Annotate your SQL with `::geometry` typecasts, if needed.
@@ -281,7 +284,7 @@ config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
281284
#### Using `github.com/twpayne/go-geom`
282285

283286
sqlc can be configured to use the [geom](https://github.com/twpayne/go-geom)
284-
package for working with PostGIS geometry types.
287+
package for working with PostGIS geometry types. See [Overriding types](../howto/overrides.md) for more information.
285288

286289
```sql
287290
-- Multipolygons in British National Grid (epsg:27700)

0 commit comments

Comments
 (0)