Skip to main content

Type mapping

A ColumnType names what the column is for, not what a database calls it. Mapping it onto a native type is the grammar's job.

ColumnTypeSQLiteMySQLPostgreSQLSQL Server
BooleanINTEGERTINYINT(1)BOOLEANBIT
TinyIntegerINTEGERTINYINTSMALLINTTINYINT
SmallIntegerINTEGERSMALLINTSMALLINTSMALLINT
IntegerINTEGERINTINTEGERINT
BigIntegerINTEGERBIGINTBIGINTBIGINT
DecimalNUMERIC(p, s)DECIMAL(p, s)NUMERIC(p, s)DECIMAL(p, s)
FloatREALFLOATREALREAL
DoubleREALDOUBLEDOUBLE PRECISIONFLOAT
CharCHAR(n)CHAR(n)CHAR(n)NCHAR(n)
StringVARCHAR(n)VARCHAR(n)VARCHAR(n)NVARCHAR(n)
TextTEXTTEXTTEXTNVARCHAR(MAX)
DateDATEDATEDATEDATE
TimeTIMETIMETIMETIME
DateTimeDATETIMEDATETIMETIMESTAMPDATETIME2
TimestampDATETIMETIMESTAMPTIMESTAMPDATETIME2
UuidCHAR(36)CHAR(36)UUIDUNIQUEIDENTIFIER
JsonTEXTJSONJSONBNVARCHAR(MAX)
BinaryBLOBBLOBBYTEAVARBINARY(MAX)

n is the length from string() or char(), defaulting to 255. p and s are the precision and scale from decimal(), defaulting to 8 and 2.

Choices worth knowing about

SQLite collapses the integers. Every integer width becomes INTEGER, because SQLite has one integer storage class and its AUTOINCREMENT is only legal on a column declared exactly INTEGER. A tinyInteger and a bigInteger are the same column there.

Timestamp is DATETIME2 on SQL Server, not TIMESTAMP. SQL Server's TIMESTAMP is a row-version type with nothing to do with time, and using it would silently give you the wrong column.

PostgreSQL gets TIMESTAMP without a time zone. If your application stores instants rather than wall-clock times, TIMESTAMPTZ is the better column and this package does not yet offer it.

Json is JSONB on PostgreSQL. Better for almost every use, but it does not preserve key order or duplicate keys. Text is the fallback on SQLite and SQL Server, which have no native JSON column.

caution

Because a type is per database, a column is not guaranteed to behave the same everywhere. Boolean is a real BOOLEAN on PostgreSQL and an INTEGER on SQLite, so a value read back is true from one and 1 from the other.