修正
This commit is contained in:
88
backend/DATABASE_MIGRATION_GUIDE.md
Normal file
88
backend/DATABASE_MIGRATION_GUIDE.md
Normal file
@@ -0,0 +1,88 @@
|
||||
# 数据库迁移指南
|
||||
|
||||
## 问题描述
|
||||
|
||||
前端代码已更新以支持新的版本追踪系统(使用 `revision_count`, `is_latest`, `original_entry_id` 字段)。但是,数据库中还没有这些字段。
|
||||
|
||||
### 错误信息
|
||||
|
||||
```
|
||||
psycopg.errors.UndefinedColumn: column je.revision_count does not exist
|
||||
```
|
||||
|
||||
## 解决方案
|
||||
|
||||
### 步骤 1:以数据库管理员身份连接
|
||||
|
||||
使用 `postgres` 超级用户或具有 DDL 权限的用户连接到数据库。
|
||||
|
||||
**在 NAS/Docker 中执行:**
|
||||
|
||||
```bash
|
||||
docker exec <postgresql_container_name> psql -U postgres -d njts_acct
|
||||
```
|
||||
|
||||
**或使用 pgAdmin:**
|
||||
|
||||
- 连接到 PostgreSQL 服务器(默认地址:`192.168.0.61:55432`)
|
||||
- 使用 postgres 用户和密码登录
|
||||
- 选择 `njts_acct` 数据库
|
||||
|
||||
### 步骤 2:执行迁移 SQL
|
||||
|
||||
复制并执行 [admin_migration_add_fields.sql](./admin_migration_add_fields.sql) 中的 SQL 代码。
|
||||
|
||||
**在 psql 中:**
|
||||
|
||||
```sql
|
||||
-- 复制 admin_migration_add_fields.sql 的所有内容并在 psql 中粘贴执行
|
||||
```
|
||||
|
||||
**或使用文件:**
|
||||
|
||||
```bash
|
||||
docker exec <postgresql_container_id> psql -U postgres -d njts_acct -f /path/to/admin_migration_add_fields.sql
|
||||
```
|
||||
|
||||
### 步骤 3:验证迁移
|
||||
|
||||
执行以下查询查看新字段:
|
||||
|
||||
```sql
|
||||
SELECT column_name, data_type, is_nullable
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'journal_entries'
|
||||
AND column_name IN ('is_latest', 'revision_count', 'original_entry_id')
|
||||
ORDER BY ordinal_position;
|
||||
```
|
||||
|
||||
应该看到 3 个新字段:
|
||||
|
||||
- `is_latest` (BOOLEAN) - 标记是否为最新版本
|
||||
- `revision_count` (INTEGER) - 修正版次数(1=原始,2+=修正版本)
|
||||
- `original_entry_id` (INTEGER) - 指向原始交易
|
||||
|
||||
## 临时解决方案(迁移前)
|
||||
|
||||
后端代码已经过修改,可以在字段不存在时继续工作:
|
||||
|
||||
- 所有仕訳将显示为最新版本(`is_latest = true`)
|
||||
- 所有仕訳的 `revision_count` 将显示为 1
|
||||
- 修改履历功能暂时不可用
|
||||
|
||||
一旦迁移完成,系统将自动启用完整的版本追踪功能。
|
||||
|
||||
## 数据库连接信息
|
||||
|
||||
```
|
||||
Host: 192.168.0.61
|
||||
Port: 55432
|
||||
Database: njts_acct
|
||||
User (普通): njts_app
|
||||
User (管理): postgres
|
||||
```
|
||||
|
||||
## 相关文件
|
||||
|
||||
- [admin_migration_add_fields.sql](./admin_migration_add_fields.sql) - 管理员执行的 SQL 迁移脚本
|
||||
- [journal_entries.py](../app/routers/journal_entries.py) - 已修改为支持字段缺失时的降级处理
|
||||
Reference in New Issue
Block a user