Skip to content

Commit 2c0db08

Browse files
authored
Merge pull request #2815 from wans10/main
fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题
2 parents 11de49f + 3229b81 commit 2c0db08

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

model/model_meta.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,21 @@ func (mi *Model) Insert() error {
4747
now := common.GetTimestamp()
4848
mi.CreatedTime = now
4949
mi.UpdatedTime = now
50-
return DB.Create(mi).Error
50+
51+
// 保存原始值(因为 Create 后可能被 GORM 的 default 标签覆盖为 1)
52+
originalStatus := mi.Status
53+
originalSyncOfficial := mi.SyncOfficial
54+
55+
// 先创建记录(GORM 会对零值字段应用默认值)
56+
if err := DB.Create(mi).Error; err != nil {
57+
return err
58+
}
59+
60+
// 使用保存的原始值进行更新,确保零值能正确保存
61+
return DB.Model(&Model{}).Where("id = ?", mi.Id).Updates(map[string]interface{}{
62+
"status": originalStatus,
63+
"sync_official": originalSyncOfficial,
64+
}).Error
5165
}
5266

5367
func IsModelNameDuplicated(id int, name string) (bool, error) {
@@ -61,11 +75,9 @@ func IsModelNameDuplicated(id int, name string) (bool, error) {
6175

6276
func (mi *Model) Update() error {
6377
mi.UpdatedTime = common.GetTimestamp()
64-
return DB.Session(&gorm.Session{AllowGlobalUpdate: false, FullSaveAssociations: false}).
65-
Model(&Model{}).
66-
Where("id = ?", mi.Id).
67-
Omit("created_time").
68-
Select("*").
78+
// 使用 Select 强制更新所有字段,包括零值
79+
return DB.Model(&Model{}).Where("id = ?", mi.Id).
80+
Select("model_name", "description", "icon", "tags", "vendor_id", "endpoints", "status", "sync_official", "name_rule", "updated_time").
6981
Updates(mi).Error
7082
}
7183

0 commit comments

Comments
 (0)