Skip to content

Commit a461f8a

Browse files
authored
docs: explain how to use the Go 1.27 stdlib uuid package via overrides (#4599)
1 parent 250f19f commit a461f8a

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

docs/reference/datatypes.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ type Author struct {
122122

123123
## UUIDs
124124

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`.
127127

128128
```sql
129129
CREATE TABLE records (
@@ -143,6 +143,70 @@ type Author struct {
143143
}
144144
```
145145

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+
146210
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`
147211
(see [Overriding types](../howto/overrides.md) for details).
148212

0 commit comments

Comments
 (0)