From 0c8db4f10558cf3693c3d55ec7f528a163ebfa21 Mon Sep 17 00:00:00 2001 From: wans10 Date: Tue, 3 Feb 2026 09:48:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor(model):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将全局更新改为字段映射更新 - 移除不必要的会话配置选项 - 使用显式字段映射替代 Omit 和 Select 操作 - 提升代码可读性和维护性 - 保持数据一致性的同时提高性能 --- model/model_meta.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/model/model_meta.go b/model/model_meta.go index 465d15f1..751965f5 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -61,12 +61,18 @@ func IsModelNameDuplicated(id int, name string) (bool, error) { func (mi *Model) Update() error { mi.UpdatedTime = common.GetTimestamp() - return DB.Session(&gorm.Session{AllowGlobalUpdate: false, FullSaveAssociations: false}). - Model(&Model{}). - Where("id = ?", mi.Id). - Omit("created_time"). - Select("*"). - Updates(mi).Error + return DB.Model(&Model{}).Where("id = ?", mi.Id).Updates(map[string]interface{}{ + "model_name": mi.ModelName, + "description": mi.Description, + "icon": mi.Icon, + "tags": mi.Tags, + "vendor_id": mi.VendorID, + "endpoints": mi.Endpoints, + "status": mi.Status, + "sync_official": mi.SyncOfficial, + "name_rule": mi.NameRule, + "updated_time": mi.UpdatedTime, + }).Error } func (mi *Model) Delete() error { From dbcf19836382b9d43454679991f829af1dd821fe Mon Sep 17 00:00:00 2001 From: wans10 Date: Tue, 3 Feb 2026 13:32:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(model):=20=E8=A7=A3=E5=86=B3=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=88=9B=E5=BB=BA=E5=92=8C=E6=9B=B4=E6=96=B0=E6=97=B6?= =?UTF-8?q?=E9=9B=B6=E5=80=BC=E5=AD=97=E6=AE=B5=E8=A2=AB=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E8=A6=86=E7=9B=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在创建记录前保存原始状态和同步官方字段值 - 使用独立的更新操作确保零值能够正确保存到数据库 - 修改更新方法使用 Select 强制更新所有字段包括零值 - 避免 GORM 默认行为对零值字段应用默认值导致数据丢失 --- model/model_meta.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/model/model_meta.go b/model/model_meta.go index 751965f5..860b9602 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -47,7 +47,21 @@ func (mi *Model) Insert() error { now := common.GetTimestamp() mi.CreatedTime = now mi.UpdatedTime = now - return DB.Create(mi).Error + + // 保存原始值(因为 Create 后可能被 GORM 的 default 标签覆盖为 1) + originalStatus := mi.Status + originalSyncOfficial := mi.SyncOfficial + + // 先创建记录(GORM 会对零值字段应用默认值) + if err := DB.Create(mi).Error; err != nil { + return err + } + + // 使用保存的原始值进行更新,确保零值能正确保存 + return DB.Model(&Model{}).Where("id = ?", mi.Id).Updates(map[string]interface{}{ + "status": originalStatus, + "sync_official": originalSyncOfficial, + }).Error } func IsModelNameDuplicated(id int, name string) (bool, error) { @@ -61,18 +75,10 @@ func IsModelNameDuplicated(id int, name string) (bool, error) { func (mi *Model) Update() error { mi.UpdatedTime = common.GetTimestamp() - return DB.Model(&Model{}).Where("id = ?", mi.Id).Updates(map[string]interface{}{ - "model_name": mi.ModelName, - "description": mi.Description, - "icon": mi.Icon, - "tags": mi.Tags, - "vendor_id": mi.VendorID, - "endpoints": mi.Endpoints, - "status": mi.Status, - "sync_official": mi.SyncOfficial, - "name_rule": mi.NameRule, - "updated_time": mi.UpdatedTime, - }).Error + // 使用 Select 强制更新所有字段,包括零值 + return DB.Model(&Model{}).Where("id = ?", mi.Id). + Select("model_name", "description", "icon", "tags", "vendor_id", "endpoints", "status", "sync_official", "name_rule", "updated_time"). + Updates(mi).Error } func (mi *Model) Delete() error {