doc-forge-reds/docs/数据库设计.md

62 lines
5.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 数据库设计
## ER 图(核心关系)
- `templates` (1) ——> (N) `generation_points`
- `generation_points` (N) ——> (1) `models`
- `templates` (1) ——> (N) `generation_tasks`
## 表结构详细
### 1. `models`AI 模型配置)
| 字段名 | 类型 | 约束 | 说明 |
| -------------- | ------------- | ------------------ | -------------------------------------- |
| id | UUID | PRIMARY KEY | 主键 |
| name | VARCHAR(100) | NOT NULL | 模型名称 |
| provider | VARCHAR(50) | NOT NULL | 供应商openai, azure, custom |
| endpoint | VARCHAR(255) | NOT NULL | 接口地址 |
| api_key | TEXT | NOT NULL | 加密存储Fernet 对称加密) |
| extra_params | JSONB | DEFAULT '{}' | 扩展参数,如 max_tokens, temperature |
| is_enabled | BOOLEAN | DEFAULT TRUE | 是否启用 |
| remark | TEXT | | 备注 |
| created_at | TIMESTAMP | DEFAULT NOW() | |
| updated_at | TIMESTAMP | DEFAULT NOW() | |
### 2. `templates`(模板文档)
| 字段名 | 类型 | 约束 | 说明 |
| -------------- | ------------- | ------------------ | ------------------------------------------ |
| id | UUID | PRIMARY KEY | |
| name | VARCHAR(200) | NOT NULL | 模板名称 |
| file_path | VARCHAR(500) | NOT NULL | 原始 .docx 文件存储路径 |
| html_content | TEXT | | 转换后的 HTML 内容(供前端编辑) |
| created_at | TIMESTAMP | DEFAULT NOW() | |
| updated_at | TIMESTAMP | DEFAULT NOW() | |
### 3. `generation_points`AI 生成点)
| 字段名 | 类型 | 约束 | 说明 |
| --------------- | ------------- | ------------------ | ------------------------------------------------- |
| id | UUID | PRIMARY KEY | |
| template_id | UUID | FOREIGN KEY | 关联模板 |
| position | JSONB | NOT NULL | 在 HTML 中的选区信息,如 {start: 100, end: 200} 或 XPath |
| prompt | TEXT | NOT NULL | 用户编写的提示词 |
| model_id | UUID | FOREIGN KEY | 指定使用的模型,若为空则使用全局默认模型 |
| ref_file_path | VARCHAR(500) | | 参考文件存储路径(可选) |
| created_at | TIMESTAMP | DEFAULT NOW() | |
| updated_at | TIMESTAMP | DEFAULT NOW() | |
### 4. `generation_tasks`(生成任务)
| 字段名 | 类型 | 约束 | 说明 |
| --------------- | ------------- | ------------------ | ----------------------------------------------- |
| id | UUID | PRIMARY KEY | |
| template_id | UUID | FOREIGN KEY | 关联模板 |
| status | VARCHAR(20) | NOT NULL | pending / processing / done / failed |
| result_file_path| VARCHAR(500) | | 生成后的 .docx 文件路径 |
| error_msg | TEXT | | 任务失败时的错误信息 |
| celery_task_id | VARCHAR(100) | | Celery 任务 ID便于追踪 |
| created_at | TIMESTAMP | DEFAULT NOW() | |
| finished_at | TIMESTAMP | | 完成时间 |
### 5. `system_config`(系统配置,可选)
存储全局默认模型 ID 等键值对。
| 字段名 | 类型 | 约束 | 说明 |
| ----------- | ------------- | -------- | -------------------- |
| key | VARCHAR(50) | PRIMARY | 配置键 |
| value | TEXT | | 配置值JSON 格式) |
| description | VARCHAR(200) | | 描述 |
## 索引建议
- `templates`:在 `created_at` 上建索引,便于按时间排序。
- `generation_points`:在 `template_id` 上建外键索引,提高关联查询速度。
- `generation_tasks`:在 `template_id,status` 上建索引,优化列表查询。