All integer types (int, tinyint, smallint, mediumint, bigint) are signed by default, allowing both negative and positive values.
Adding { unsigned: true } restricts the column to non-negative values only but doubles the upper range.
integer
A 4-byte integer with a range of -2,147,483,648 to 2,147,483,647 signed, or 0 to 4,294,967,295 unsigned
For more info please refer to the official MySQL docs.
BINARY(M) stores a fixed-length byte string of exactly M bytes (If omitted, M defaults to 1).
On insert, shorter values are right-padded with 0x00 bytes to reach M bytes; on retrieval, no padding is stripped.
All bytes are significant in comparisons, including ORDER BY and DISTINCT operations
For more info please refer to the official MySQL docs.
VARBINARY(M) stores a variable-length byte string of up to M bytes (M as required).
For VARBINARY, there is no padding for inserts and no bytes are stripped for retrievals.
All bytes are significant in comparisons, including ORDER BY and DISTINCT operations
For more info please refer to the official MySQL docs.
// will be inferred as `Buffer`longblob: longblob({ mode: "buffer" }),// will be inferred as `string`longblob: longblob({ mode: "string" }),
The buffer mode is the default way to work with blobs.
Drizzle will represent the blob value as a Buffer instance
The string mode represents blob values as UTF-8 encoded strings,
which can be useful when storing text data in blob columns
---
char
The length of a CHAR(M) column is fixed to the length that you declare when you create the table. M can be any value from 0 to 255, default is 1
When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { char, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { char: char(),});// will be inferred as char: "value1" | "value2" | nullchar: char({ enum: ["value1", "value2"] })
CREATE TABLE `table` ( `char` char);
varchar
Values in VARCHAR columns are variable-length strings. VARCHAR(M) stores up to M characters, where M ranges from 0 to 65,535VARCHAR values are not padded when they are stored. Trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { varchar: varchar({ length: 2 }),});// will be inferred as text: "value1" | "value2" | nullvarchar: varchar({ length: 6, enum: ["value1", "value2"] })
CREATE TABLE `table` ( `varchar` varchar(2));
text
A TEXT column with a maximum length of 65,535 characters (64 KB)
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { text, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { text: text(),});// will be inferred as text: "value1" | "value2" | nulltext: text({ enum: ["value1", "value2"] });
CREATE TABLE `table` ( `text` text);
tinytext
A TEXT column with a maximum length of 255 characters
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { tinytext, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { tinytext: tinytext(),});// will be inferred as tinytext: "value1" | "value2" | nulltinytext: tinytext({ enum: ["value1", "value2"] });
CREATE TABLE `table` ( `tinytext` tinytext);
mediumtext
A TEXT column with a maximum length of 16,777,215 characters (16 MB)
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { mediumtext, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { mediumtext: mediumtext(),});// will be inferred as mediumtext: "value1" | "value2" | nullmediumtext: mediumtext({ enum: ["value1", "value2"] });
CREATE TABLE `table` ( `mediumtext` mediumtext);
longtext
A TEXT column with a maximum length of 4,294,967,295 characters (4 GB)
For more info please refer to the official MySQL docs.
You can define { enum: ["value1", "value2"] } config to infer insert and select types, it won’t check runtime values.
import { longtext, mysqlTable } from "drizzle-orm/mysql-core";export const table = mysqlTable('table', { longtext: longtext(),});// will be inferred as longtext: "value1" | "value2" | nulllongtext: longtext({ enum: ["value1", "value2"] });
CREATE TABLE `table` ( `longtext` longtext);
---
boolean
BOOLEAN is a synonym for TINYINT(1). A value of 0 is considered false, non-zero values are considered true
For more info please refer to the official MySQL docs.
You can specify either date or string infer modes:
// will infer as datedate: date({ mode: "date" }),// will infer as stringdate: date({ mode: "string" }),
datetime
A date and time value in 'YYYY-MM-DD hh:mm:ss' format, with a range of '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. Supports fractional seconds up to 6 digits
For more info please refer to the official MySQL docs.
A time value in 'hh:mm:ss' format, with a range of '-838:59:59' to '838:59:59'. Can represent time of day or elapsed time. Supports fractional seconds up to 6 digits
For more info please refer to the official MySQL docs.
A date and time value in 'YYYY-MM-DD hh:mm:ss' format, with a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. Supports fractional seconds up to 6 digits
For more info please refer to the official MySQL docs.
A native JSON data type that enables efficient access to data in JSON documents. JSON documents are automatically validated and stored in an optimized binary format that permits quick read access to document elements
For more info please refer to the official MySQL docs.
You can specify .$type<..>() for json object inference, it won’t check runtime values.
It provides compile time protection for default values, insert and select schemas.
// will be inferred as { foo: string }json: json().$type<{ foo: string }>();// will be inferred as string[]json: json().$type<string[]>();// won't compilejson: json().$type<string[]>().default({});
---
enum
A string object with a value chosen from a list of permitted values that are enumerated at table creation time.
For more info please refer to the official MySQL docs.
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.
import { int, json, mysqlTable } from "drizzle-orm/mysql-core";type UserId = number & { __brand: "user_id" };type Data = { foo: string; bar: number;};export const users = mysqlTable("users", { id: int().$type<UserId>().primaryKey(), jsonField: json().$type<Data>(),});
Not null
NOT NULL constraint dictates that the associated column may not contain a NULL 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.
CREATE TABLE `table` ( `int` int DEFAULT 3, `int2` int DEFAULT (3));
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