From cabc3c3442cfcd8f6fbe9946acef2db380a6c94e Mon Sep 17 00:00:00 2001 From: yuliang_guo Date: Sat, 31 Jan 2026 17:13:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BB=83=E4=B9=A0?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E6=97=B6=E7=9A=84DetachedInstanceError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在第二次commit后refresh session对象 - 避免异步session管理导致的对象脱离错误 --- backend/app/api/v1/practice.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backend/app/api/v1/practice.py b/backend/app/api/v1/practice.py index a8887f2..aa1e3a9 100644 --- a/backend/app/api/v1/practice.py +++ b/backend/app/api/v1/practice.py @@ -724,8 +724,15 @@ async def end_practice_session( new_badges = await badge_service.check_and_award_badges(current_user.id) await db.commit() + # 第二次commit后需要refresh,避免DetachedInstanceError + await db.refresh(session) except Exception as e: logger.warning(f"练习经验值/奖章处理失败: {str(e)}") + # 确保 session 仍然可用 + try: + await db.refresh(session) + except Exception: + pass return ResponseModel( code=200,