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
+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)