fix(database): 解决服务启动时数据库表结构同步问题

- 在 serve 命令启动时自动执行数据库迁移,确保表结构同步
- 添加 AutoMigrate 调用防止源码列与数据库列不一致导致的运行时错误
- 保持幂等性,只创建新表或添加缺失列
- 移除多余的空行以改善代码格式
- 添加字符串包导入支持相关功能
```
This commit is contained in:
SunYF
2026-09-19 17:43:14 +08:00
parent 05b0b8a909
commit 5a33028003
2 changed files with 9 additions and 3 deletions
+4 -3
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"bj_power_mes/ent"
@@ -400,8 +401,9 @@ func (s *Service) ListMaterialRequests(ctx context.Context, o MaterialRequestQue
// ReceiveMaterialRequest 接料确认(两个入口共用:工位终端扫码接料 / MES 备料页人工点「已接料」)。
//
// 字段口径(2026-09-19 定稿):写 receivedQty(已接料),**不再累加 sentQty**。
// sentQty 已发出量 [写者:自动出库] —— 幂等基准「还需发多少 = reqQty sentQty」
// receivedQty 已接料量 [写者:本方法] —— 在途 = sentQty receivedQty
//
// sentQty 已发出量 [写者:自动出库] —— 幂等基准「还需发多少 = reqQty − sentQty
// receivedQty 已接料量 [写者:本方法] —— 在途 = sentQty receivedQty
//
// 上限 = 已发出量(工位只能接到仓库已发出来的料),不超接。
// qty<=0 表示「确认本批全部剩余」,适合扫码/一键接料;>0 为本次实收批次量。
@@ -446,7 +448,6 @@ func (s *Service) ReceiveMaterialRequest(ctx context.Context, requestNo string,
return nil
}
// SetMaterialRequestStatus 推进备料单状态(WMS 下发 AGV DELIVERING / 到位 DONE
func (s *Service) SetMaterialRequestStatus(ctx context.Context, requestNo, status string) error {
n, err := s.ctx.EntClient.MaterialRequest.Update().
+5
View File
@@ -149,6 +149,11 @@ func main() {
return
case "", "serve":
// 正常启动服务
// 启动时自动建表/补列(幂等):ent Schema.Create 只建新表,既有表新列走 schemaPatchSQL。
// 之前 serve 不跑迁移导致「源码已加列、库里没有」的 42703 运行时报错(2026-09-19 实录)。
if err := db.AutoMigrate(c.Database); err != nil {
panic(err)
}
default:
fmt.Println("未知命令: " + cmd + ";可用命令: migrate / reset-all / seed / serve")
os.Exit(1)