Content Types
字段类型
17 种字段类型、通用选项、类型专属配置和验证规则。
内容类型支持 17 种字段类型。每个字段通过 [[fields]] 定义,包含 field_type 和可选约束。
字段类型一览
| 类型 | SQLite | MySQL | PostgreSQL | 适用场景 |
|---|---|---|---|---|
text | TEXT | VARCHAR(255) | VARCHAR(255) | 短文本 — 标题、名称、URL |
richtext | TEXT | TEXT | TEXT | 长文本 — HTML、Markdown |
integer | INTEGER | INT | INTEGER | 32 位整数 |
bigint | INTEGER | BIGINT | BIGINT | 64 位整数 |
decimal | TEXT | DECIMAL(16,4) | NUMERIC(16,4) | 精确小数 — 价格、金额 |
float | REAL | DOUBLE | DOUBLE PRECISION | 浮点数 |
boolean | BOOLEAN | TINYINT(1) | BOOLEAN | 布尔值 |
date | TEXT | DATE | DATE | 日期(ISO 8601) |
datetime | TEXT | DATETIME | TIMESTAMPTZ(0) | 时间戳(ISO 8601) |
time | TEXT | TIME | TIMETZ | 时间(ISO 8601) |
email | TEXT | VARCHAR(255) | VARCHAR(255) | 邮箱地址(格式验证) |
password | TEXT | VARCHAR(255) | VARCHAR(255) | 密码字符串 |
enum | TEXT | VARCHAR(255) | VARCHAR(255) | 固定值列表 |
uid | TEXT | VARCHAR(255) | VARCHAR(255) | 自动生成的 slug / UID |
json | TEXT | JSON | JSONB | 任意结构化数据 |
media | TEXT | VARCHAR(255) | VARCHAR(255) | 文件 / 图片上传 |
relation | INTEGER | BIGINT | BIGINT | 关联记录引用 |
通用选项
所有字段类型共享以下选项:
[[fields]]
name = "title"
field_type = "text"
required = true
unique = true
default = "Untitled"
private = false
immutable = false
label = "Title"
description = "The display title"
max_length = 200
min = 0
max = 100
pattern = "^[A-Z]"| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
required | bool | false | 创建时必填 |
unique | bool | false | 唯一约束 |
default | any | — | 默认值(字符串、数字、布尔、null) |
private | bool | false | 公开 API 隐藏,仅管理 API 可见 |
immutable | bool | false | 创建后不可修改 |
label | string | — | 管理后台显示标签 |
description | string | — | 字段描述 |
max_length | int | — | 字符串最大长度 |
min | float | — | 数值最小值 |
max | float | — | 数值最大值 |
pattern | string | — | 正则验证模式 |
类型专属选项
enum
必须提供 enum_values:
[[fields]]
name = "priority"
field_type = "enum"
enum_values = ["low", "medium", "high"]
default = "medium"uid
从另一个字段自动生成 URL 安全的 slug:
[[fields]]
name = "slug"
field_type = "uid"
target_field = "title"
unique = truemedia
控制接受的文件类型和数量:
[[fields]]
name = "cover"
field_type = "media"
media_config = { accept = ["image/*"], max_count = 1 }| 选项 | 说明 |
|---|---|
accept | MIME 类型模式数组(如 ["image/*", "application/pdf"]) |
max_count | 最大文件数量(默认 1) |
relation
详见 关联关系。
[[fields]]
name = "author"
field_type = "relation"
relation = { relation_type = "many_to_one", target = "users" }验证规则
系统在每次创建和更新时自动验证输入:
| 检查 | 时机 | 方式 |
|---|---|---|
| required | 创建 | 字段必须存在且非空 |
| immutable | 更新 | 更新请求中不能包含该字段 |
| 类型检查 | 创建 / 更新 | 值类型必须匹配字段类型 |
| enum_values | 创建 / 更新 | 值必须在允许列表中 |
| max_length | 创建 / 更新 | 字符串长度不能超限 |
| min / max | 创建 / 更新 | 数值范围验证 |
| pattern | 创建 / 更新 | 值必须匹配正则表达式 |
| unique | 创建 / 更新 | 数据库唯一性检查(更新时排除自身) |
验证失败返回 422 Unprocessable Entity,附带字段级详情:
{
"error": "Validation failed",
"details": {
"email": "must be a valid email address",
"age": "must be greater than or equal to 18"
}
}索引
在字段之外单独定义数据库索引:
[[indexes]]
fields = ["slug"]
unique = true
[[indexes]]
fields = ["status", "created_at"]| 选项 | 类型 | 说明 |
|---|---|---|
fields | string[] | 要索引的列名 |
unique | bool | 是否为唯一索引(默认 false) |
