本部分文档中的所有示例不使用数据库列名别名,列名是根据 TypeScript 键生成的。
如果你想,可以在列名中使用数据库别名,也可以使用 casing 参数为 Drizzle 定义映射策略。
你可以在 这里 阅读更多信息。
我们对所有这些类型提供原生支持,如果这还不足以满足你的需求,请随意创建 自定义类型。
本部分文档中的所有示例不使用数据库列名别名,列名是根据 TypeScript 键生成的。
如果你想,可以在列名中使用数据库别名,也可以使用 casing 参数为 Drizzle 定义映射策略。
你可以在 这里 阅读更多信息。
一个有符号整数,存储在 0、1、2、3、4、6 或 8 字节中,具体取决于值的大小。
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int()
});CREATE TABLE `table` (
`int` int
);import { tinyint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
tinyint: tinyint()
});CREATE TABLE `table` (
`tinyint` tinyint
);import { smallint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
smallint: smallint()
});CREATE TABLE `table` (
`smallint` smallint
);import { mediumint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
mediumint: mediumint()
});CREATE TABLE `table` (
`mediumint` mediumint
);import { bigint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
bigint: bigint({ mode: 'number' })
bigintUnsigned: bigint({ mode: 'number', unsigned: true })
});
bigint('...', { mode: 'number' | 'bigint' });
// 你还可以为 bigint 指定 unsigned 选项
bigint('...', { mode: 'number' | 'bigint', unsigned: true })CREATE TABLE `table` (
`bigint` bigint,
`bigintUnsigned` bigint unsigned
);我们省略了 bigint(M) 中的 M 配置,因为它表示数字类型的显示宽度。
import { real, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
real: real()
});CREATE TABLE `table` (
`real` real
);import { real, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
realPrecision: real({ precision: 1,}),
realPrecisionScale: real({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`realPrecision` real(1),
`realPrecisionScale` real(1, 1)
);import { decimal, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
decimal: decimal(),
decimalNum: decimal({ scale: 30, mode: 'number' }),
decimalBig: decimal({ scale: 30, mode: 'bigint' }),
});CREATE TABLE `table` (
`decimal` decimal,
`decimalNum` decimal(30),
`decimalBig` decimal(30)
);import { decimal, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
decimalPrecision: decimal({ precision: 1,}),
decimalPrecisionScale: decimal({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`decimalPrecision` decimal(1),
`decimalPrecisionScale` decimal(1, 1)
);import { double, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
double: double('double')
});CREATE TABLE `table` (
`double` double
);import { double, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
doublePrecision: double({ precision: 1,}),
doublePrecisionScale: double({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`doublePrecision` double(1),
`doublePrecisionScale` double(1, 1)
);import { float, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
float: float()
});CREATE TABLE `table` (
`float` float
);SERIAL 是 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE 的别名。
import { serial, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
serial: serial()
});CREATE TABLE `table` (
`serial` serial AUTO_INCREMENT
);BINARY(M) stores a fixed-length byte string of exactly M bytes.
On insert, shorter values are right-padded with 0x00 bytes to reach M bytes; on retrieval, no padding is stripped.
All bytes—including trailing 0x00—are significant in comparisons, ORDER BY, and DISTINCT
import { binary, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
binary: binary()
});CREATE TABLE `table` (
`binary` binary
);VARBINARY(M) stores a variable-length byte string of exactly M bytes.
On insert, shorter values are right-padded with 0x00 bytes to reach M bytes; on retrieval, no padding is stripped.
All bytes—including trailing 0x00—are significant in comparisons, ORDER BY, and DISTINCT
import { varbinary, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
varbinary: varbinary({ length: 2}),
});CREATE TABLE `table` (
`varbinary` varbinary(2)
);import { char, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
char: char(),
});CREATE TABLE `table` (
`char` char
);你可以定义 { enum: ["value1", "value2"] } 配置来推断 insert 和 select 类型,但它 不会 检查运行时的值。
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
varchar: varchar({ length: 2 }),
});
// 将推断为文本: "value1" | "value2" | null
varchar: varchar({ length: 6, enum: ["value1", "value2"] })CREATE TABLE `table` (
`varchar` varchar(2)
);你可以定义 { enum: ["value1", "value2"] } 配置来推断 insert 和 select 类型,但它 不会 检查运行时的值。
import { text, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
text: text(),
});
// 将推断为文本: "value1" | "value2" | null
text: text({ enum: ["value1", "value2"] });CREATE TABLE `table` (
`text` text
);import { boolean, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
boolean: boolean(),
});CREATE TABLE `table` (
`boolean` boolean
);import { boolean, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
date: date(),
});CREATE TABLE `table` (
`date` date
);import { datetime, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
datetime: datetime(),
});
datetime('...', { mode: 'date' | "string"}),
datetime('...', { fsp : 0..6}),CREATE TABLE `table` (
`datetime` datetime
);import { datetime, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
datetime: datetime({ mode: 'date', fsp: 6 }),
});CREATE TABLE `table` (
`datetime` datetime(6)
);import { time, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
time: time(),
timefsp: time({ fsp: 6 }),
});
time('...', { fsp: 0..6 }),CREATE TABLE `table` (
`time` time,
`timefsp` time(6)
);import { year, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
year: year(),
});CREATE TABLE `table` (
`year` year
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp(),
});
timestamp('...', { mode: 'date' | "string"}),
timestamp('...', { fsp : 0..6}),CREATE TABLE `table` (
`timestamp` timestamp
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp({ mode: 'date', fsp: 6 }),
});CREATE TABLE `table` (
`timestamp` timestamp(6)
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp().defaultNow(),
});CREATE TABLE `table` (
`timestamp` timestamp DEFAULT (now())
);import { json, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
json: json(),
});
CREATE TABLE `table` (
`json` json
);你可以指定 .$type<..>() 进行 json 对象推断,它 不会 检查运行时值。
它在编译时提供默认值、插入和选择模式的保护。
// 将推断为 { foo: string }
json: json().$type<{ foo: string }>();
// 将推断为 string[]
json: json().$type<string[]>();
// 将无法编译
json: json().$type<string[]>().default({});import { mysqlEnum, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
popularity: mysqlEnum(['unknown', 'known', 'popular']),
});CREATE TABLE `table` (
`popularity` enum('unknown','known','popular')
);每个列构建器都有一个 .$type() 方法,允许你自定义列的数据类型。这在处理未知或品牌类型时非常有用。
type UserId = number & { __brand: 'user_id' };
type Data = {
foo: string;
bar: number;
};
const users = mysqlTable('users', {
id: int().$type<UserId>().primaryKey(),
jsonField: json().$type<Data>(),
});NOT NULL 约束规定相关联的列不能包含 NULL 值。
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().notNull(),
});CREATE TABLE `table` (
`int` int NOT NULL
);DEFAULT 子句指定在用户进行
INSERT 时如果不显式提供值,则使用的列的默认值。
如果没有显式的 DEFAULT 子句附加到列定义,
则列的默认值为 NULL。
一个显式的 DEFAULT 子句可以指定默认值为 NULL、
字符串常量、blob 常量、有符号数字或任何用括号括起来的常量表达式。
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().default(3),
});CREATE TABLE `table` (
`int` int DEFAULT 3
);使用 $default() 或 $defaultFn() 时,它们只是同一函数的不同别名,
你可以在运行时生成默认值,并在所有插入查询中使用这些值。
这些函数可以帮助你利用各种实现,例如 uuid、cuid、cuid2 等等。
注意:该值不会影响 drizzle-kit 行为,仅在 drizzle-orm 的运行时使用。
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
import { createId } from '@paralleldrive/cuid2';
const table = mysqlTable('table', {
id: varchar({ length: 128 }).$defaultFn(() => createId()),
});使用 $onUpdate() 或 $onUpdateFn() 时,它们也是同一函数的不同别名,
你可以在运行时生成默认值,并在所有更新查询中使用这些值。
向列添加动态更新值。当更新行时将调用该函数, 如果没有提供值,则返回的值将用作列值。 如果未提供默认值(或 $defaultFn),则在插入行时也将调用该函数, 并且返回的值将用作列值。
注意:该值不会影响 drizzle-kit 行为,仅在 drizzle-orm 的运行时使用。
import { text, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
alwaysNull: text().$type<string | null>().$onUpdate(() => null),
});import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().primaryKey(),
});CREATE TABLE `table` (
`int` int PRIMARY KEY NOT NULL
);import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().autoincrement(),
});CREATE TABLE `table` (
`int` int AUTO_INCREMENT
);