dify二次開發之數據庫表設計
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 | 更新時間 |
索引和約束
各個表都定義了適當的索引和約束來提高查詢性能和保證數據完整性:
- 主鍵約束:每個表都有主鍵
- 唯一約束:保證特定字段組合的唯一性
- 索引:為經常查詢的字段建立索引
關系圖
erDiagram
tenants ||--o{ tenant_account_joins : contains
accounts ||--o{ tenant_account_joins : contains
tenants ||--o{ datasets : contains
accounts ||--o{ datasets : creates
datasets ||--o{ documents : contains
documents ||--o{ document_segments : contains
tenants ||--o{ apps : contains
accounts ||--o{ apps : creates
apps ||--o{ app_model_configs : has
apps ||--o{ conversations : has
conversations ||--o{ messages : contains
accounts ||--o{ messages : sends
datasets ||--o{ app_dataset_joins : joins
apps ||--o{ app_dataset_joins : joins
總結
Dify 的數據庫設計采用了規范化的結構,通過多個表來存儲不同類型的數據,并通過外鍵關系建立表之間的聯系。這種設計有利于數據的一致性和完整性,同時也便于擴展和維護。

浙公網安備 33010602011771號