Skip to content

Commit 247ed12

Browse files
committed
docs: document db_type values for type overrides
## Summary Documents the db_type strings sqlc matches for PostgreSQL, MySQL, and SQLite type overrides, how exact matching works, and common pitfalls (pg_catalog prefixes, nullability). Fixes #2762 Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com>
1 parent 4e9fe78 commit 247ed12

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

docs/howto/overrides.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,111 @@ sql:
116116
pointer: true
117117
```
118118

119+
## Choosing the right `db_type` value
120+
121+
Overrides match with an **exact string comparison** against the type name sqlc
122+
infers for a column. That name is `schema.name` when the catalog includes a
123+
schema, otherwise just `name` (see
124+
[sdk.DataType](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/sdk/sdk.go)).
125+
126+
This is why the same logical type can appear under several aliases, and why a
127+
value that works in SQL (`timestamptz`) sometimes needs a catalog-qualified form
128+
in config (`pg_catalog.timestamptz`).
129+
130+
### How to discover the type sqlc sees
131+
132+
1. Prefer a `column` override when you only care about one field — it bypasses
133+
`db_type` naming entirely (`table.column`, or `schema.table.column`).
134+
2. For a database-wide override, generate once and inspect the Go field type, or
135+
check the switch cases in the engine type mappers linked below.
136+
3. When an override seems ignored, try the `pg_catalog.`-prefixed form (Postgres)
137+
or the lowercase SQL type name (MySQL/SQLite). Configure **both** nullable and
138+
non-nullable overrides if needed.
139+
140+
### PostgreSQL
141+
142+
Common `db_type` strings (all aliases in a row match the same mapper branch).
143+
Prefer the **bold** form when more than one is listed — that is usually what the
144+
catalog emits:
145+
146+
| SQL / concept | Accepted `db_type` values |
147+
| --- | --- |
148+
| `smallint` | `pg_catalog.int2`, `smallint`, `int2` |
149+
| `integer` | `pg_catalog.int4`, `integer`, `int`, `int4` |
150+
| `bigint` | `pg_catalog.int8`, `bigint`, `int8` |
151+
| `smallserial` | `pg_catalog.serial2`, `smallserial`, `serial2` |
152+
| `serial` | `pg_catalog.serial4`, `serial`, `serial4` |
153+
| `bigserial` | `pg_catalog.serial8`, `bigserial`, `serial8` |
154+
| `real` | `pg_catalog.float4`, `real`, `float4` |
155+
| `double precision` | `pg_catalog.float8`, `float`, `double precision`, `float8` |
156+
| `numeric` / `decimal` | `pg_catalog.numeric`, `numeric`, `money` |
157+
| `boolean` | `pg_catalog.bool`, `boolean`, `bool` |
158+
| `text` / `varchar` / `char` | `text`, `pg_catalog.varchar`, `pg_catalog.bpchar`, `string`, `citext`, `name` |
159+
| `bytea` | `pg_catalog.bytea`, `bytea`, `blob` |
160+
| `date` | `date` |
161+
| `time` | `pg_catalog.time` |
162+
| `timetz` | `pg_catalog.timetz` |
163+
| `timestamp` | `pg_catalog.timestamp`, `timestamp` |
164+
| `timestamptz` | **`pg_catalog.timestamptz`**, `timestamptz` |
165+
| `interval` | `pg_catalog.interval`, `interval` |
166+
| `uuid` | `uuid` |
167+
| `json` | `pg_catalog.json`, `json` |
168+
| `jsonb` | `pg_catalog.jsonb`, `jsonb` |
169+
| `inet` / `cidr` | `inet`, `cidr` |
170+
| `macaddr` | `macaddr`, `macaddr8` |
171+
| ranges | `int4range`, `int8range`, `numrange`, `tsrange`, `tstzrange`, `daterange` (and `*multirange` variants) |
172+
| geometric | `point`, `line`, `lseg`, `box`, `path`, `polygon`, `circle` |
173+
| other | `bit`, `varbit`, `pg_catalog.bit`, `pg_catalog.varbit`, `hstore`, `ltree`, `vector`, `void`, `any` |
174+
175+
Full branch list:
176+
[postgresql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/postgresql_type.go).
177+
178+
### MySQL
179+
180+
| SQL / concept | Accepted `db_type` values |
181+
| --- | --- |
182+
| string | `varchar`, `text`, `char`, `tinytext`, `mediumtext`, `longtext` |
183+
| `tinyint` | `tinyint` (often used for booleans) |
184+
| `smallint` | `smallint` |
185+
| `int` | `int`, `integer`, `mediumint` |
186+
| `bigint` | `bigint`, `bigint unsigned`, `bigint signed` |
187+
| `year` | `year` |
188+
| binary | `blob`, `binary`, `varbinary`, `tinyblob`, `mediumblob`, `longblob` |
189+
| floating | `double`, `double precision`, `real`, `float` |
190+
| decimal | `decimal`, `dec`, `fixed` |
191+
| `enum` | `enum` |
192+
| date/time | `date`, `timestamp`, `datetime`, `time` |
193+
| boolean | `boolean`, `bool` |
194+
| `json` | `json` |
195+
| other | `any` |
196+
197+
Use `unsigned: true` on the override when matching unsigned numeric columns.
198+
Full branch list:
199+
[mysql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/mysql_type.go).
200+
201+
### SQLite
202+
203+
| SQL / concept | Accepted `db_type` values |
204+
| --- | --- |
205+
| integer | `int`, `integer`, `tinyint`, `smallint`, `mediumint`, `bigint`, `unsignedbigint`, `int2`, `int8` |
206+
| blob | `blob` |
207+
| real | `real`, `double`, `doubleprecision`, `float` |
208+
| boolean | `boolean`, `bool` |
209+
| date/time | `date`, `datetime`, `timestamp` |
210+
| json | `json`, `jsonb` |
211+
| other | `any` |
212+
213+
Full branch list:
214+
[sqlite_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/sqlite_type.go).
215+
216+
### Tips when an override does not apply
217+
218+
- **Postgres timestamps:** try `pg_catalog.timestamptz` / `pg_catalog.timestamp`, not only the short name.
219+
- **Nullability:** a non-nullable override never applies to a nullable column (and the reverse). Duplicate the entry with `nullable: true`.
220+
- **Arrays / slices:** element `db_type` still uses the base type name; see [Datatypes](../reference/datatypes.md#arrays).
221+
- **Expressions / functions:** the inferred type may differ from the underlying column (for example aggregates). Prefer a `column` override or cast in SQL when that happens.
222+
223+
119224
## Global overrides
120225

121226
To override types in all packages that `sqlc` generates, add an override

0 commit comments

Comments
 (0)