fix(settings): use deep copy to fix SQLAlchemy change detection
SQLAlchemy wasn't detecting changes when we modified the dict in place and re-assigned the same object reference. Using deep copy ensures the ORM sees the update. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -49,12 +49,17 @@ async def update_user_profile(
|
||||
|
||||
async def update_llm_config(user_id: str, config: dict, db: AsyncSession) -> dict:
|
||||
"""更新 LLM 配置"""
|
||||
import copy
|
||||
logger.info(f"update_llm_config called with config keys: {list(config.keys())}")
|
||||
logger.info(f"chat config: {config.get('chat')}")
|
||||
result = await db.execute(select(User).where(User.id == user_id))
|
||||
user = result.scalar_one_or_none()
|
||||
if not user:
|
||||
raise ValueError("用户不存在")
|
||||
|
||||
current = user.llm_config or {}
|
||||
# 创建深拷贝,避免 SQLAlchemy 变更检测问题
|
||||
current = copy.deepcopy(user.llm_config) or {}
|
||||
logger.info(f"current llm_config before update: {current}")
|
||||
# 合并配置 - 直接替换整个类型配置列表
|
||||
for key, value in config.items():
|
||||
if value is not None:
|
||||
@@ -69,8 +74,11 @@ async def update_llm_config(user_id: str, config: dict, db: AsyncSession) -> dic
|
||||
current[key] = value
|
||||
else:
|
||||
current[key] = value
|
||||
logger.info(f"current llm_config after update: {current}")
|
||||
user.llm_config = current
|
||||
await db.commit()
|
||||
await db.refresh(user)
|
||||
logger.info(f"user.llm_config after refresh: {user.llm_config}")
|
||||
return current
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user