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
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`
9
7
instead.
10
8
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
+
11
12
```yaml
12
13
version: "2"
13
14
sql:
@@ -17,34 +18,44 @@ sql:
17
18
gen:
18
19
go:
19
20
package: "authors"
20
-
out: "postgresql"
21
+
out: "db"
22
+
sql_package: "pgx/v5"
21
23
overrides:
22
24
- db_type: "uuid"
23
-
go_type: "github.com/gofrs/uuid.UUID"
25
+
go_type:
26
+
import: "github.com/google/uuid"
27
+
type: "UUID"
24
28
```
25
29
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:
27
33
28
34
- `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_catalogprefixed 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.
30
36
- `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.
32
38
- `go_type`:
33
-
- A fullyqualified 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.
34
40
- `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.
37
43
- `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`.
39
47
40
48
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.
43
54
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
46
56
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:
48
59
49
60
- `import`:
50
61
- 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
53
64
- `type`:
54
65
- The type name itself, without any package prefix.
55
66
- `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.
57
68
- `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.
0 commit comments