🐛 fix(model): allow zero-value updates so tags/description can be cleared

This commit is contained in:
t0ng7u
2025-08-10 11:03:39 +08:00
parent f942361f7b
commit ca1f3c6e4c

View File

@@ -73,11 +73,14 @@ func IsModelNameDuplicated(id int, name string) (bool, error) {
// Update 更新现有模型记录
func (mi *Model) Update() error {
// 仅更新需要变更的字段,避免覆盖 CreatedTime
mi.UpdatedTime = common.GetTimestamp()
// 排除 created_time其余字段自动更新避免新增字段时需要维护列表
return DB.Model(&Model{}).Where("id = ?", mi.Id).Omit("created_time").Updates(mi).Error
// 使用 Session 配置并选择所有字段,允许零值(如空字符串)也能被更新
return DB.Session(&gorm.Session{AllowGlobalUpdate: false, FullSaveAssociations: false}).
Model(&Model{}).
Where("id = ?", mi.Id).
Omit("created_time").
Select("*").
Updates(mi).Error
}
// Delete 软删除模型记录