Merge pull request #2815 from wans10/main

fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题
This commit is contained in:
Seefs
2026-02-08 19:56:16 +08:00
committed by GitHub

View File

@@ -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,11 +75,9 @@ 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("*").
// 使用 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
}