Skip to content

Type mappings

How the plugin maps SQL types to Python types, per engine. These are the built-in defaults; a type override or converter replaces the type for a specific column or SQL type.

How a column’s type is built

The tables below give the base Python type for a SQL type. Two modifiers are applied on top, based on the column:

  • Nullable (the column is not NOT NULL): | None is appended, e.g. str | None.
  • Array / list (a Postgres array column, or a sqlc.slice parameter): the base type is wrapped as collections.abc.Sequence[T].

Both can combine: a nullable bytea[] becomes collections.abc.Sequence[memoryview] | None.

PostgreSQL

SQL typePython type
smallint, integer, bigint (and int2/int4/int8, smallserial/serial/bigserial, pg_catalog.* forms)int
real, double precision (float4/float8)float
numericdecimal.Decimal
moneystr
boolean (bool)bool
json, jsonbstr
byteamemoryview
datedatetime.date
time, timetzdatetime.time
timestamp, timestamptzdatetime.datetime
intervaldatetime.timedelta
text, varchar, char/bpchar, citextstr
uuiduuid.UUID
inet, cidr, macaddr, macaddr8str
ltree, lquery, ltxtquerystr
a user-defined ENUM typethe generated enum class, e.g. enums.Mood
anything elsetyping.Any
A SQL type the plugin does not recognize falls back to typing.Any. If that happens for a type you use often, add a type override or converter so it gets a real Python type.

SQLite

SQLite type names are matched case-insensitively, by exact name first and then by prefix (so varchar(255) matches varchar, and decimal(10,5) matches decimal).

SQL typePython type
int, integer, tinyint, smallint, mediumint, bigint, int2, int8, bigserialint
real, double, double precision, float, numericfloat
decimal, decimal(p,s)decimal.Decimal
bool, booleanbool
datedatetime.date
datetime, timestampdatetime.datetime
text, clob, json, character, varchar, varyingcharacter, nchar, nativecharacter, nvarcharstr
blobmemoryview
anything elsetyping.Any

For the two SQLite drivers, several of these types also need runtime adapters and converters (registered in the generated code) to round-trip correctly - see SQLite type conversion.