SQLite column types
Based on the official SQLite docs,
each value stored in an SQLite database (or manipulated by the database engine)
has one of the following storage classes NULL
, INTEGER
, REAL
, TEXT
and BLOB
.
We have native support for all of them, yet if that’s not enough for you, feel free to create custom types.
Integer
A signed integer, stored in 0
, 1
, 2
, 3
, 4
, 6
, or 8
bytes depending on the magnitude of the value.
Real
A floating point value, stored as an 8-byte IEEE
floating point number.
Text
A text string, stored using the database encoding (UTF-8
, UTF-16BE
or UTF-16LE
).
You can define { enum: ["value1", "value2"] }
config to infer insert
and select
types, it won’t check runtime values.
Blob
A blob of data, stored exactly as it was input.
It’s recommended to use text('', { mode: 'json' })
instead of blob('', { mode: 'json' })
,
because it supports JSON functions:
All JSON functions currently throw an error if any of their arguments are BLOBs because BLOBs are reserved for a future enhancement in which BLOBs will store the binary encoding for JSON.
You can specify .$type<..>()
for blob inference, it won’t check runtime values.
It provides compile time protection for default values, insert and select schemas.
Boolean
SQLite does not have native boolean
data type, yet you can specify integer
column to be in a boolean
mode.
This allows you to operate boolean values in your code and Drizzle stores them as 0 and 1 integer
values in the database.
Bigint
Since there is no bigint
data type in SQLite, Drizzle offers a special bigint
mode for blob
columns.
This mode allows you to work with BigInt instances in your code, and Drizzle stores them as blob values in the database.
Customizing column data type
Every column builder has a .$type()
method, which allows you to customize the data type of the column. This is useful, for example, with unknown or branded types.
Columns constraints
Not null
NOT NULL
constraint dictates that the associated column may not contain a NULL
value.
Default value
The DEFAULT
clause specifies a default value to use for the column if no value
is explicitly provided by the user when doing an INSERT
.
If there is no explicit DEFAULT
clause attached to a column definition,
then the default value of the column is NULL
.
An explicit DEFAULT
clause may specify that the default value is NULL
,
a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses.
A default value may also be one of the special case-independent keywords CURRENT_TIME
, CURRENT_DATE
or CURRENT_TIMESTAMP
.
When using $default()
or $defaultFn()
, which are simply different aliases for the same function,
you can generate defaults at runtime and use these values in all insert queries.
These functions can assist you in utilizing various implementations such as uuid
, cuid
, cuid2
, and many more.
Note: This value does not affect the drizzle-kit
behavior, it is only used at runtime in drizzle-orm
When using $onUpdate()
or $onUpdateFn()
, which are simply different aliases for the same function,
you can generate defaults at runtime and use these values in all update queries.
Adds a dynamic update value to the column. The function will be called when the row is updated, and the returned value will be used as the column value if none is provided. If no default (or $defaultFn) value is provided, the function will be called when the row is inserted as well, and the returned value will be used as the column value.
Note: This value does not affect the drizzle-kit
behavior, it is only used at runtime in drizzle-orm