MySQL 列类型

我们原生支持所有这些类型,但如果这还不够,您可以随意创建 自定义类型

重要提示

本文档的所有示例均不使用数据库列名别名,列名均由 TypeScript 键生成。

您可以根据需要使用数据库列名别名,也可以使用 casing 参数为 Drizzle 定义映射策略。

您可以在此处了解更多信息。

integer

一种有符号整数,根据值的量级,以 0123468 字节存储。

import { int, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	int: int()
});
CREATE TABLE `table` (
	`int` int
);

tinyint

import { tinyint, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	tinyint: tinyint()
});
CREATE TABLE `table` (
	`tinyint` tinyint
);

smallint

import { smallint, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	smallint: smallint()
});
CREATE TABLE `table` (
	`smallint` smallint
);

mediumint

import { mediumint, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	mediumint: mediumint()
});
CREATE TABLE `table` (
	`mediumint` mediumint
);

bigint

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' });

// You can also specify unsigned option for bigint
bigint('...', { mode: 'number' | 'bigint', unsigned: true })
CREATE TABLE `table` (
	`bigint` bigint,
	`bigintUnsigned` bigint unsigned
);

我们省略了 `bigint(M)` 中 `M` 的配置,因为它表示数字类型的显示宽度。

---

real

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)
);

decimal

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)
);

double

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)
);

float

import { float, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	float: float()
});
CREATE TABLE `table` (
	`float` float
);

---

serial

SERIALBIGINT 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

BINARY(M) 存储长度固定、正好 `M` 字节的字节串。
插入时,较短的值将用 0x00 字节进行右填充,以达到 M 字节;检索时,不移除任何填充。所有字节(包括尾随的 0x00)在比较、ORDER BYDISTINCT 中都具有重要意义。

import { binary, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	binary: binary()
});
CREATE TABLE `table` (
	`binary` binary
);

varbinary

VARBINARY(M) 存储长度可变、正好 M 字节的字节串。
插入时,较短的值将用 0x00 字节进行右填充,以达到 M 字节;检索时,不移除任何填充。所有字节(包括尾随的 0x00)在比较、ORDER BYDISTINCT 中都具有重要意义。

import { varbinary, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	varbinary: varbinary({ length: 2}),
});
CREATE TABLE `table` (
	`varbinary` varbinary(2)
);

---

char

import { char, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	char: char(),
});
CREATE TABLE `table` (
	`char` char
);

varchar

您可以定义 { enum: ["value1", "value2"] } 配置来推断 insertselect 类型,但它**不会**检查运行时值。

import { varchar, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	varchar: varchar({ length: 2 }),
});

// will be inferred as text: "value1" | "value2" | null
varchar: varchar({ length: 6, enum: ["value1", "value2"] })
CREATE TABLE `table` (
	`varchar` varchar(2)
);

text

您可以定义 { enum: ["value1", "value2"] } 配置来推断 insertselect 类型,但它**不会**检查运行时值。

import { text, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	text: text(),
});

// will be inferred as text: "value1" | "value2" | null
text: text({ enum: ["value1", "value2"] });
CREATE TABLE `table` (
	`text` text
);

---

boolean

import { boolean, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	boolean: boolean(),
});
CREATE TABLE `table` (
	`boolean` boolean
);

---

date

import { boolean, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	date: date(),
});
CREATE TABLE `table` (
	`date` date
);

datetime

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)
);

time

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)
);

year

import { year, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	year: year(),
});
CREATE TABLE `table` (
	`year` year
);

timestamp

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())
);

---

json

import { json, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
	json: json(),
});
CREATE TABLE `table` (
	`json` json
);

您可以为 JSON 对象推断指定 .$type<..>(),它**不会**检查运行时值。它为默认值、插入和选择模式提供了编译时保护。

// will be inferred as { foo: string }
json: json().$type<{ foo: string }>();

// will be inferred as string[]
json: json().$type<string[]>();

// won't compile
json: json().$type<string[]>().default({});

---

enum

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() 方法,允许您自定义列的数据类型。这对于未知类型或品牌类型(branded types)等情况非常有用。

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() 时(它们只是同一个函数的不同别名),您可以在运行时生成默认值,并将这些值用于所有插入查询。这些函数可以帮助您利用各种实现,例如 uuidcuidcuid2 等。

注意:此值不影响 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
);