MySQL 列类型

我们对所有列类型都有原生支持,如果这还不够,请随意创建 自定义类型

整数类型

有符号整数,根据值的大小存储在 0123468 字节中。

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

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

tinyint 类型

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

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

smallint 类型

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

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

mediumint 类型

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

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

bigint 类型

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

const table = mysqlTable('table', {
  bigint: bigint('bigint', { mode: 'number' })
  bigintUnsigned: bigint('bigintU', { mode: 'number', unsigned: true })
});

bigint('...', { mode: 'number' | 'bigint' });

// 也可以为 bigint 指定 unsigned 选项
bigint('...', { mode: 'number' | 'bigint', unsigned: true })
CREATE TABLE `table` (
  `bigint` bigint,
  `bigintU` bigint unsigned,
);

We’ve omitted config of M in bigint(M), since it indicates the display width of the numeric type and

real 类型

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

const table = mysqlTable('table', {
  real: real('real')
});
CREATE TABLE `table` (
  `real` real,
);
import { real, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
  realPrecision: real('real_precision', { precision: 1,}),
  realPrecisionScale: real('real_precision_scale', { precision: 1, scale: 1,}),
});
CREATE TABLE `table` (
  `real_precision` real(1),
  `real_precision_scale` real(1, 1),
);

decimal 类型

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

const table = mysqlTable('table', {
  decimal: decimal('decimal')
});
CREATE TABLE `table` (
  `decimal` decimal,
);
import { decimal, mysqlTable } from "drizzle-orm/mysql-core";

const table = mysqlTable('table', {
  decimalPrecision: decimal('decimal_precision', { precision: 1,}),
  decimalPrecisionScale: decimal('decimal_precision_scale', { precision: 1, scale: 1,}),
});
CREATE TABLE `table` (
  `decimal_precision` decimal(1),
  `decimal_precision_scale` 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('double_precision', { precision: 1,}),
  doublePrecisionScale: double('double_precision_scale', { precision: 1, scale: 1,}),
});
CREATE TABLE `table` (
  `double_precision` double(1),
  `double_precision_scale` double(1, 1),
);

float 类型

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

const table = mysqlTable('table', {
  float: 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('serial')
});
CREATE TABLE `table` (
  `serial` serial AUTO_INCREMENT,
);

binary 类型

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

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

varbinary 类型

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

const table = mysqlTable('table', {
  varbinary: 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('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('varchar', { length: 2 }),
});

// 会被推断为 text: "value1" | "value2" | null
varchar: 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('text'),
});

// 会被推断为 text: "value1" | "value2" | null
text: 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('boolean'),
});
CREATE TABLE `table` (
  `boolean` boolean,
);

date 类型

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

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

datetime 类型

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

const table = mysqlTable('table', {
  datetime: 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('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('time'),
  timefsp: time('time_fsp', { fsp: 6 }),
});
  
time('...', { fsp: 0..6 }),
CREATE TABLE `table` (
  `time` time,
  `time_fsp` time(6),
);

year 类型

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

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

timestamp 类型

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

const table = mysqlTable('table', {
  timestamp: 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('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('timestamp').defaultNow(),
});
CREATE TABLE `table` (
  `timestamp` timestamp DEFAULT (now()),
);

json 类型

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

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

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

// 会被推断为 { foo: string }
json: json('json').$type<{ foo: string }>();

// 会被推断为 string[]
json: json('json').$type<string[]>();

// 不会被编译
json: json('json').$type<string[]>().default({});

枚举列

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

const table = mysqlTable('table', {
  mysqlEnum: mysqlEnum('popularity', ['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('id').$type<UserId>().primaryKey(),
  jsonField: json('json_field').$type<Data>(),
});

列约束

Not null 约束

NOT NULL 约束指定关联列不允许包含 NULL 值。

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

const table = mysqlTable('table', {
  int: 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('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('id', { 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('always_null').$type<string | null>().$onUpdate(() => null),
});

主键约束

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

const table = mysqlTable('table', {
  int: 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('int').autoincrement(),
});
CREATE TABLE `table` (
  `int` int AUTO_INCREMENT
);
Become a Gold Sponsor