From ca1f3c6e4c00acb387423c72becc42ad48af6f4c Mon Sep 17 00:00:00 2001 From: t0ng7u Date: Sun, 10 Aug 2025 11:03:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(model):=20allow=20zero-value?= =?UTF-8?q?=20updates=20so=20tags/description=20can=20be=20cleared?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/model_meta.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/model/model_meta.go b/model/model_meta.go index 63b6800e..371e3137 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -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 软删除模型记录