我们对所有列类型都有原生支持,如果这还不够,请随意创建 自定义类型。
整数类型
有符号整数,根据值的大小存储在 0
、1
、2
、3
、4
、6
或 8
字节中。
tinyint 类型
smallint 类型
mediumint 类型
bigint 类型
We’ve omitted config of M
in bigint(M)
, since it indicates the display width of the numeric type and
real 类型
decimal 类型
double 类型
float 类型
serial 类型
SERIAL
是 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
的别名。
binary 类型
varbinary 类型
char 类型
varchar 类型
你可以使用 { enum: ["value1", "value2"] }
配置来推断 insert
和 select
类型,它不会检查运行时的值。
text 类型
你可以使用 { enum: ["value1", "value2"] }
配置来推断 insert
和 select
类型,它不会检查运行时的值。
boolean 类型
date 类型
datetime 类型
time 类型
year 类型
timestamp 类型
json 类型
您可以为 json 对象推断指定 .$type<..>()
,它不会检查运行时的值。
它为默认值、插入和选择模式提供了编译时保护。
枚举列
自定义列数据类型
每个列构建器都有一个 .$type()
方法,允许您自定义列的数据类型。这在处理未知或品牌类型时非常有用。
列约束
Not null 约束
NOT NULL
约束指定关联列不允许包含 NULL
值。
默认值
DEFAULT
子句指定在执行 INSERT
时,如果用户没有显式提供值,则使用的列的默认值。
如果列定义没有明确的 DEFAULT
子句,那么列的默认值是 NULL
。
显式的 DEFAULT
子句可以指定默认值是 NULL
、字符串常量、blob 常量、有符号数字或括在括号中的任何常量表达式。
当使用 $default()
或 $defaultFn()
时,它们只是相同函数的不同别名,
您可以在运行时生成默认值并在所有插入查询中使用这些值。
这些函数可以帮助您利用各种实现,如 uuid
、cuid
、cuid2
等等。
ℹ️
注意:此值不会影响 drizzle-kit
的行为,它仅在 drizzle-orm
中运行时使用
当使用 $onUpdate()
或 $onUpdateFn()
时,它们只是相同函数的不同别名,
您可以在运行时生成默认值并在所有更新查询中使用这些值。
为列添加一个动态的更新值。当行更新时,将调用该函数,并且如果没有提供列值,则使用返回的值作为列值。
如果未提供默认值(或 $defaultFn
)值,则在插入行时也将调用该函数,并将返回的值用作列值。
ℹ️
注意:此值不会影响 drizzle-kit
的行为,它仅在 drizzle-orm
中运行时使用
主键约束
自增约束