You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.. note:: Type overrides and field renaming are only fully-supported for Go.
4
+
3
5
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.
8
12
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:
11
18
12
19
```yaml
13
20
version: "2"
@@ -22,11 +29,19 @@ sql:
22
29
sql_package: "pgx/v5"
23
30
overrides:
24
31
- db_type: "uuid"
32
+
nullable: true
25
33
go_type:
26
34
import: "github.com/google/uuid"
27
35
type: "UUID"
36
+
- column: "users.birthday"
37
+
go_type: "time.Time"
28
38
```
29
39
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
+
30
45
## The `overrides` list
31
46
32
47
Each element in the `overrides` list has the following keys:
@@ -39,21 +54,23 @@ Each element in the `overrides` list has the following keys:
39
54
- 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.
40
55
- `go_struct_tag`:
41
56
- 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.
43
58
- `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`.
46
62
- `nullable`:
47
63
- If `true`, sqlc will apply this override when a column is nullable.
48
64
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`.
50
67
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.
54
72
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.
57
74
58
75
### The `go_type` map
59
76
@@ -80,7 +97,7 @@ sql:
80
97
queries: "postgresql/query.sql"
81
98
engine: "postgresql"
82
99
gen:
83
-
go:
100
+
go:
84
101
package: "authors"
85
102
out: "db"
86
103
sql_package: "pgx/v5"
@@ -92,3 +109,74 @@ sql:
92
109
type: "MyType"
93
110
pointer: true
94
111
```
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:
Copy file name to clipboardExpand all lines: docs/reference/config.md
+7-101Lines changed: 7 additions & 101 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,36 +194,11 @@ The `gen` mapping supports the following keys:
194
194
- `rename`:
195
195
- Customize the name of generated struct fields. See [Renaming fields](../howto/rename.md) for usage information.
196
196
- `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.
198
198
199
199
##### overrides
200
200
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.
227
202
228
203
#### kotlin
229
204
@@ -356,7 +331,7 @@ overrides:
356
331
rename:
357
332
id: "Identifier"
358
333
overrides:
359
-
- db_type: "timestamptz"
334
+
- db_type: "pg_catalog.timestamptz"
360
335
nullable: true
361
336
engine: "postgresql"
362
337
go_type:
@@ -368,7 +343,7 @@ sql:
368
343
queries: "postgresql/query.sql"
369
344
engine: "postgresql"
370
345
gen:
371
-
go:
346
+
go:
372
347
package: "authors"
373
348
out: "postgresql"
374
349
- schema: "mysql/schema.sql"
@@ -443,6 +418,8 @@ Each mapping in the `packages` collection has the following keys:
443
418
- Either `postgresql` or `mysql`. Defaults to `postgresql`.
444
419
- `sql_package`:
445
420
- 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.
446
423
- `emit_db_tags`:
447
424
- If true, add DB tags to generated structs. Defaults to `false`.
448
425
- `emit_prepared_queries`:
@@ -494,78 +471,7 @@ Each mapping in the `packages` collection has the following keys:
494
471
495
472
### overrides
496
473
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.
Copy file name to clipboardExpand all lines: docs/reference/datatypes.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
database types to Go types. Choices for more complex types are described below.
5
5
6
6
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.
8
8
9
9
## Arrays
10
10
@@ -143,7 +143,9 @@ type Author struct {
143
143
}
144
144
```
145
145
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
+
147
149
```json
148
150
{
149
151
"overrides": [
@@ -158,7 +160,8 @@ For MySQL, there is no native `uuid` data type. When using `UUID_TO_BIN` to stor
158
160
## JSON
159
161
160
162
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).
162
165
The `pgx` implementation will marshal/unmarshal the struct automatically.
163
166
164
167
```go
@@ -209,7 +212,7 @@ type Book struct {
209
212
210
213
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.
211
214
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).
213
216
214
217
## Geometry
215
218
@@ -222,7 +225,7 @@ package for working with PostGIS geometry types in [GEOS](https://libgeos.org/).
222
225
223
226
There are three steps:
224
227
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).
226
229
2. Call `github.com/twpayne/pgx-geos.Register` on each
227
230
`*github.com/jackc/pgx/v5.Conn`.
228
231
3. Annotate your SQL with `::geometry` typecasts, if needed.
0 commit comments