mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-05-05 05:31:45 +00:00
Merge pull request #2815 from wans10/main
fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题
This commit is contained in:
@@ -47,7 +47,21 @@ func (mi *Model) Insert() error {
|
|||||||
now := common.GetTimestamp()
|
now := common.GetTimestamp()
|
||||||
mi.CreatedTime = now
|
mi.CreatedTime = now
|
||||||
mi.UpdatedTime = 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) {
|
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 {
|
func (mi *Model) Update() error {
|
||||||
mi.UpdatedTime = common.GetTimestamp()
|
mi.UpdatedTime = common.GetTimestamp()
|
||||||
return DB.Session(&gorm.Session{AllowGlobalUpdate: false, FullSaveAssociations: false}).
|
// 使用 Select 强制更新所有字段,包括零值
|
||||||
Model(&Model{}).
|
return DB.Model(&Model{}).Where("id = ?", mi.Id).
|
||||||
Where("id = ?", mi.Id).
|
Select("model_name", "description", "icon", "tags", "vendor_id", "endpoints", "status", "sync_official", "name_rule", "updated_time").
|
||||||
Omit("created_time").
|
|
||||||
Select("*").
|
|
||||||
Updates(mi).Error
|
Updates(mi).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user