Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions model/model_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down