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
Copy file name to clipboardExpand all lines: docs/reference/datatypes.md
+66-2Lines changed: 66 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -122,8 +122,8 @@ type Author struct {
122
122
123
123
## UUIDs
124
124
125
-
The Go standard library does not come with a `uuid` package. For UUID support,
126
-
sqlc uses the excellent `github.com/google/uuid` package. The pgx/v5 sql package uses `pgtype.UUID`.
125
+
For UUID support, sqlc uses the `github.com/google/uuid` package by default.
126
+
The pgx/v5 sql package uses `pgtype.UUID`.
127
127
128
128
```sql
129
129
CREATETABLErecords (
@@ -143,6 +143,70 @@ type Author struct {
143
143
}
144
144
```
145
145
146
+
### Using the standard library uuid package
147
+
148
+
Go 1.27 added a [`uuid`](https://pkg.go.dev/uuid) package to the standard
149
+
library. sqlc will use this package by default in a future release. Until
150
+
then, use it today with type overrides
151
+
(see [Overriding types](../howto/overrides.md) for details).
152
+
153
+
Two overrides are required: one for non-nullable columns and one for nullable
154
+
columns. The standard library does not include a `NullUUID` type, so nullable
155
+
columns map to `*uuid.UUID` instead.
156
+
157
+
```yaml
158
+
version: "2"
159
+
sql:
160
+
- engine: "postgresql"
161
+
schema: "schema.sql"
162
+
queries: "query.sql"
163
+
gen:
164
+
go:
165
+
package: "db"
166
+
out: "db"
167
+
sql_package: "pgx/v5"
168
+
overrides:
169
+
- db_type: "uuid"
170
+
go_type: "uuid.UUID"
171
+
- db_type: "uuid"
172
+
nullable: true
173
+
go_type:
174
+
import: "uuid"
175
+
type: "UUID"
176
+
pointer: true
177
+
```
178
+
179
+
With this configuration, a table with both nullable and non-nullable `uuid`
180
+
columns:
181
+
182
+
```sql
183
+
CREATE TABLE records (
184
+
id uuid PRIMARY KEY,
185
+
external_id uuid
186
+
);
187
+
```
188
+
189
+
generates:
190
+
191
+
```go
192
+
package db
193
+
194
+
import (
195
+
"uuid"
196
+
)
197
+
198
+
type Record struct {
199
+
ID uuid.UUID
200
+
ExternalID *uuid.UUID
201
+
}
202
+
```
203
+
204
+
These overrides work with both the `pgx/v5` and `database/sql` sql packages.
205
+
pgx supports any type whose underlying type is `[16]byte`, and as of Go 1.27
206
+
`database/sql` converts `uuid.UUID` values in both directions: parameters are
207
+
bound via `driver.DefaultParameterConverter` and results are scanned into
208
+
`uuid.UUID`and `*uuid.UUID` destinations, with `nil` representing `NULL`.
209
+
146
210
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
211
(see [Overriding types](../howto/overrides.md) for details).
0 commit comments