Skip to content

fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题 - #2815

Merged
seefs001 merged 2 commits into
QuantumNous:mainfrom
wans10:main
Feb 8, 2026
Merged

fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题#2815
seefs001 merged 2 commits into
QuantumNous:mainfrom
wans10:main

Conversation

@wans10

@wans10 wans10 commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

问题描述

模型管理模块中,创建或编辑模型时,将"参与官方同步"或"状态"开关关闭后保存,刷新页面后开关仍显示为开启状态。

根本原因

1. Insert 方法问题

当使用 DB.Create(mi) 创建模型时,GORM 会因为 default:1 标签将零值字段(status=0, sync_official=0)替换为默认值 1,并回写到结构体。后续的更新操作使用的是已被覆盖的值。

2. Update 方法问题

原始代码使用 Updates(mi) 更新模型,但 GORM 的 Updates() 方法默认忽略零值字段,导致 status=0sync_official=0 不会被写入数据库。

解决方案

Insert 方法

在调用 DB.Create() 之前保存 statussync_official 的原始值,创建后使用保存的原始值进行二次更新。

Update 方法

使用 GORM 的 Select() 方法显式指定要更新的字段,强制更新包括零值在内的所有字段。

修改文件

  • model/model_meta.go
    • Insert(): 保存原始值后再更新
    • Update(): 使用 Select() 指定更新字段

测试验证

  1. 创建新模型时关闭两个开关 → 保存后刷新页面,确认开关保持关闭状态 ✅
  2. 编辑现有模型,将开关从开启切换为关闭 → 保存后刷新页面,确认状态正确 ✅
  3. 反向测试:将开关从关闭切换为开启 → 保存后刷新页面,确认状态正确 ✅

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where zero-value data was not being preserved correctly when creating and updating records. Records now properly retain default values and empty field states during save operations.

- 将全局更新改为字段映射更新
- 移除不必要的会话配置选项
- 使用显式字段映射替代 Omit 和 Select 操作
- 提升代码可读性和维护性
- 保持数据一致性的同时提高性能
- 在创建记录前保存原始状态和同步官方字段值
- 使用独立的更新操作确保零值能够正确保存到数据库
- 修改更新方法使用 Select 强制更新所有字段包括零值
- 避免 GORM 默认行为对零值字段应用默认值导致数据丢失
@coderabbitai

coderabbitai Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The model_meta.go file's Insert and Update methods were refactored to handle GORM defaults and zero-values more precisely. Insert now uses a create-then-update sequence to preserve original Status and SyncOfficial values. Update switches from session-based controls to a direct Model update with explicit field selection.

Changes

Cohort / File(s) Summary
Data Persistence Logic
model/model_meta.go
Insert method changed from single-create to create-then-update flow to preserve zero-value Status and SyncOfficial fields. Update method replaced session-based approach with direct Model update specifying explicit field list (model_name, description, icon, tags, vendor_id, endpoints, status, sync_official, name_rule, updated_time) via targeted Select for zero-value inclusion.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop, skip, and update in the database store,
Where zeros are treasured and defaults adore,
Insert then refine—oh what clever design!
Fields preserved with precision so fine,
Status and sync in their rightful place! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is in Chinese and refers to fixing an issue with model management where 'Official Sync' and 'Status' switches fail to save properly. This directly matches the PR objectives which document this exact problem and its solution.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001
seefs001 merged commit 2c0db08 into QuantumNous:main Feb 8, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
fix: 修复模型管理"参与官方同步"与"状态"开关无法保存的问题
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants