Dify 数据库表结构文档
概述
Dify 项目使用 SQLAlchemy 作为 ORM 框架,数据库表结构定义在 [api/models](file:///Users/chunlin/Desktop/dify_redevelop/dify-1.8.1/api/models) 目录下的 Python 文件中。本文档将详细说明各个主要表的结构和用途。
主要表结构
1. 账户相关表
accounts 表
存储用户账户信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 账户ID,主键 |
| name | String(255) | 用户名 |
| String(255) | 邮箱,唯一索引 | |
| password | String(255) | 加密密码 |
| password_salt | String(255) | 密码盐值 |
| avatar | String(255) | 头像URL |
| interface_language | String(255) | 界面语言 |
| interface_theme | String(255) | 界面主题 |
| timezone | String(255) | 时区 |
| last_login_at | DateTime | 最后登录时间 |
| last_login_ip | String(255) | 最后登录IP |
| last_active_at | DateTime | 最后活跃时间 |
| status | String(16) | 账户状态 |
| initialized_at | DateTime | 初始化时间 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
tenants 表
存储租户(工作空间)信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 租户ID,主键 |
| name | String(255) | 租户名称 |
| encrypt_public_key | Text | 加密公钥 |
| plan | String(255) | 订阅计划 |
| status | String(255) | 租户状态 |
| custom_config | Text | 自定义配置 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
tenant_account_joins 表
存储租户与账户的关联关系。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 关联ID,主键 |
| tenant_id | StringUUID | 租户ID |
| account_id | StringUUID | 账户ID |
| current | Boolean | 是否为当前租户 |
| role | String(16) | 角色(owner, admin, editor, normal, dataset_operator) |
| invited_by | StringUUID | 邀请人ID |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
account_integrates 表
存储账户第三方集成信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 集成ID,主键 |
| account_id | StringUUID | 账户ID |
| provider | String(16) | 提供商 |
| open_id | String(255) | 第三方开放ID |
| encrypted_token | String(255) | 加密令牌 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
invitation_codes 表
存储邀请码信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | Integer | ID |
| batch | String(255) | 批次 |
| code | String(32) | 邀请码 |
| status | String(16) | 状态 |
| used_at | DateTime | 使用时间 |
| used_by_tenant_id | StringUUID | 使用者租户ID |
| used_by_account_id | StringUUID | 使用者账户ID |
| deprecated_at | DateTime | 废弃时间 |
| created_at | DateTime | 创建时间 |
2. 应用相关表
apps 表
存储应用信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 应用ID,主键 |
| tenant_id | StringUUID | 租户ID |
| name | String(255) | 应用名称 |
| description | Text | 应用描述 |
| mode | String(255) | 应用模式(completion, workflow, chat等) |
| icon_type | String(255) | 图标类型 |
| icon | String(255) | 图标 |
| icon_background | String(255) | 图标背景色 |
| app_model_config_id | StringUUID | 应用模型配置ID |
| workflow_id | StringUUID | 工作流ID |
| status | String(255) | 状态 |
| enable_site | Boolean | 是否启用站点 |
| enable_api | Boolean | 是否启用API |
| api_rpm | Integer | API每分钟请求数限制 |
| api_rph | Integer | API每小时请求数限制 |
| is_demo | Boolean | 是否为演示应用 |
| is_public | Boolean | 是否公开 |
| is_universal | Boolean | 是否通用应用 |
| tracing | Text | 追踪配置 |
| max_active_requests | Integer | 最大并发请求数 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_by | StringUUID | 更新者ID |
| updated_at | DateTime | 更新时间 |
| use_icon_as_answer_icon | Boolean | 是否使用图标作为回答图标 |
3. 数据集相关表
datasets 表
存储数据集信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 数据集ID,主键 |
| tenant_id | StringUUID | 租户ID |
| name | String(255) | 数据集名称 |
| description | Text | 数据集描述 |
| provider | String(255) | 提供商 |
| permission | String(255) | 权限设置 |
| data_source_type | String(255) | 数据源类型 |
| indexing_technique | String(255) | 索引技术 |
| index_struct | Text | 索引结构 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_by | StringUUID | 更新者ID |
| updated_at | DateTime | 更新时间 |
| embedding_model | String(255) | 嵌入模型 |
| embedding_model_provider | String(255) | 嵌入模型提供商 |
| collection_binding_id | StringUUID | 集合绑定ID |
| retrieval_model | JSONB | 检索模型配置 |
| built_in_field_enabled | Boolean | 是否启用内置字段 |
documents 表
存储文档信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 文档ID,主键 |
| tenant_id | StringUUID | 租户ID |
| dataset_id | StringUUID | 数据集ID |
| position | Integer | 位置 |
| data_source_type | String(255) | 数据源类型 |
| data_source_info | JSONB | 数据源信息 |
| dataset_process_rule_id | StringUUID | 数据集处理规则ID |
| batch | String(255) | 批次 |
| name | String(255) | 文档名称 |
| created_from | String(255) | 创建来源 |
| created_by | StringUUID | 创建者ID |
| segments_count | Integer | 段落数 |
| hit_count | Integer | 命中次数 |
| word_count | Integer | 字数 |
| indexing_status | String(255) | 索引状态 |
| completed_at | DateTime | 完成时间 |
| updated_at | DateTime | 更新时间 |
| indexing_latency | Float | 索引延迟 |
| enabled | Boolean | 是否启用 |
| disabled_at | DateTime | 禁用时间 |
| archived | Boolean | 是否归档 |
| display_status | String(255) | 显示状态 |
| doc_form | String(255) | 文档格式 |
| doc_language | String(255) | 文档语言 |
| external_id | String(255) | 外部ID |
document_segments 表
存储文档段落信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 段落ID,主键 |
| tenant_id | StringUUID | 租户ID |
| dataset_id | StringUUID | 数据集ID |
| document_id | StringUUID | 文档ID |
| position | Integer | 位置 |
| content | Text | 内容 |
| answer | Text | 回答 |
| word_count | Integer | 字数 |
| tokens | Integer | 令牌数 |
| keywords | JSONB | 关键词 |
| index_node_id | String(255) | 索引节点ID |
| index_node_hash | String(255) | 索引节点哈希 |
| hit_count | Integer | 命中次数 |
| enabled | Boolean | 是否启用 |
| disabled_at | DateTime | 禁用时间 |
| disabled_by | StringUUID | 禁用者ID |
| status | String(255) | 状态 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_by | StringUUID | 更新者ID |
| updated_at | DateTime | 更新时间 |
| indexing_at | DateTime | 索引时间 |
| completed_at | DateTime | 完成时间 |
| error | Text | 错误信息 |
| stopped_at | DateTime | 停止时间 |
4. 工作流相关表
workflows 表
存储工作流信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 工作流ID,主键 |
| tenant_id | StringUUID | 租户ID |
| app_id | StringUUID | 应用ID |
| type | String(255) | 工作流类型 |
| version | String(255) | 版本 |
| marked_name | String | 标记名称 |
| marked_comment | String | 标记注释 |
| graph | Text | 工作流图结构(JSON) |
| features | Text | 特性配置 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_by | StringUUID | 更新者ID |
| updated_at | DateTime | 更新时间 |
| environment_variables | Text | 环境变量 |
| conversation_variables | Text | 对话变量 |
workflow_runs 表
存储工作流运行实例信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 运行ID,主键 |
| tenant_id | StringUUID | 租户ID |
| app_id | StringUUID | 应用ID |
| workflow_id | StringUUID | 工作流ID |
| sequence_number | Integer | 序号 |
| workflow_nodes | JSONB | 工作流节点 |
| inputs | JSONB | 输入参数 |
| status | String(255) | 状态 |
| outputs | JSONB | 输出结果 |
| error | Text | 错误信息 |
| elapsed_time | Float | 耗时 |
| total_tokens | Integer | 总令牌数 |
| total_price | Float | 总价格 |
| currency | String(255) | 货币 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
| created_by_role | String(255) | 创建者角色 |
| created_by | StringUUID | 创建者ID |
| finished_at | DateTime | 完成时间 |
workflow_node_executions 表
存储工作流节点执行信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 执行ID,主键 |
| tenant_id | StringUUID | 租户ID |
| app_id | StringUUID | 应用ID |
| workflow_id | StringUUID | 工作流ID |
| triggered_from | String(255) | 触发来源 |
| index | Integer | 索引 |
| predecessor_node_id | String(255) | 前驱节点ID |
| node_id | String(255) | 节点ID |
| node_type | String(255) | 节点类型 |
| title | String(255) | 标题 |
| inputs | JSONB | 输入参数 |
| process_data | JSONB | 处理数据 |
| outputs | JSONB | 输出结果 |
| status | String(255) | 状态 |
| error | Text | 错误信息 |
| elapsed_time | Float | 耗时 |
| execution_metadata | JSONB | 执行元数据 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
| finished_at | DateTime | 完成时间 |
5. 模型相关表
providers 表
存储模型提供商信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 提供商ID,主键 |
| tenant_id | StringUUID | 租户ID |
| provider_name | String(255) | 提供商名称 |
| provider_type | String(255) | 提供商类型 |
| encrypted_config | Text | 加密配置 |
| is_valid | Boolean | 是否有效 |
| last_used | DateTime | 最后使用时间 |
| quota_type | String(255) | 配额类型 |
| quota_limit | BigInteger | 配额限制 |
| quota_used | BigInteger | 已用配额 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
provider_models 表
存储模型信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 模型ID,主键 |
| tenant_id | StringUUID | 租户ID |
| .provider_id | StringUUID | 提供商ID |
| name | String(255) | 模型名称 |
| model_name | String(255) | 模型名称 |
| model_type | String(255) | 模型类型 |
| encrypted_config | Text | 加密配置 |
| is_valid | Boolean | 是否有效 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
6. 对话相关表
conversations 表
存储对话信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 对话ID,主键 |
| app_id | StringUUID | 应用ID |
| app_model_config_id | StringUUID | 应用模型配置ID |
| model_provider | String(255) | 模型提供商 |
| override_model_configs | Text | 覆盖模型配置 |
| model_id | String(255) | 模型ID |
| mode | String(255) | 模式 |
| name | String(255) | 对话名称 |
| summary | Text | 摘要 |
| inputs | JSONB | 输入参数 |
| introduction | Text | 介绍 |
| system_instruction | Text | 系统指令 |
| system_instruction_role | String(255) | 系统指令角色 |
| provider_id | StringUUID | 提供商ID |
| created_by_role | String(255) | 创建者角色 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
messages 表
存储消息信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | 消息ID,主键 |
| app_id | StringUUID | 应用ID |
| conversation_id | StringUUID | 对话ID |
| from_source | String(255) | 来源 |
| from_end_user_id | StringUUID | 终端用户ID |
| from_account_id | StringUUID | 账户ID |
| to_end_user_id | StringUUID | 目标终端用户ID |
| to_account_id | StringUUID | 目标账户ID |
| role | String(255) | 角色 |
| content | Text | 内容 |
| message_metadata | Text | 消息元数据 |
| message_annotations | Text | 消息注释 |
| retriever_resources | Text | 检索资源 |
| message_files | Text | 消息文件 |
| status | String(255) | 状态 |
| error | Text | 错误信息 |
| parent_message_id | StringUUID | 父消息ID |
| agent_based | Boolean | 是否基于代理 |
| model_provider | String(255) | 模型提供商 |
| model_id | String(255) | 模型ID |
| provider_id | StringUUID | 提供商ID |
| prompt_messages | Text | 提示消息 |
| prompt_tokens | Integer | 提示令牌数 |
| answer_tokens | Integer | 回答令牌数 |
| answer | Text | 回答 |
| currency | String(255) | 货币 |
| total_price | Float | 总价格 |
| message_price | Float | 消息价格 |
| answer_price | Float | 回答价格 |
| latency | Float | 延迟 |
| created_by_role | String(255) | 创建者角色 |
| created_by | StringUUID | 创建者ID |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
7. 工具相关表
tool_builtin_providers 表
存储内置工具提供商信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | ID,主键 |
| name | String(256) | 名称 |
| tenant_id | StringUUID | 租户ID |
| user_id | StringUUID | 用户ID |
| provider | String(256) | 提供商 |
| encrypted_credentials | Text | 加密凭证 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
| is_default | Boolean | 是否默认 |
| credential_type | String(32) | 凭证类型 |
| expires_at | BigInteger | 过期时间 |
tool_api_providers 表
存储API工具提供商信息。
| 字段 | 类型 | 描述 |
|---|---|---|
| id | StringUUID | ID,主键 |
| name | String(255) | 名称 |
| icon | String(255) | 图标 |
| schema | Text | 原始模式 |
| schema_type_str | String(40) | 模式类型 |
| user_id | StringUUID | 用户ID |
| tenant_id | StringUUID | 租户ID |
| description | Text | 描述 |
| tools_str | Text | 工具JSON字符串 |
| credentials_str | Text | 凭证JSON字符串 |
| privacy_policy | String(255) | 隐私政策 |
| custom_disclaimer | Text | 自定义声明 |
| created_at | DateTime | 创建时间 |
| updated_at | DateTime | 更新时间 |
索引和约束
各个表都定义了适当的索引和约束来提高查询性能和保证数据完整性:
- 主键约束:每个表都有主键
- 唯一约束:保证特定字段组合的唯一性
- 索引:为经常查询的字段建立索引
关系图
erDiagramtenants ||--o{ tenant_account_joins : containsaccounts ||--o{ tenant_account_joins : containstenants ||--o{ datasets : containsaccounts ||--o{ datasets : createsdatasets ||--o{ documents : containsdocuments ||--o{ document_segments : containstenants ||--o{ apps : containsaccounts ||--o{ apps : createsapps ||--o{ app_model_configs : hasapps ||--o{ conversations : hasconversations ||--o{ messages : containsaccounts ||--o{ messages : sendsdatasets ||--o{ app_dataset_joins : joinsapps ||--o{ app_dataset_joins : joins
总结
Dify 的数据库设计采用了规范化的结构,通过多个表来存储不同类型的数据,并通过外键关系建立表之间的联系。这种设计有利于数据的一致性和完整性,同时也便于扩展和维护。
