From ece6a70c6503f4be4de9bbdf2368f36c92a917db Mon Sep 17 00:00:00 2001 From: Windpicker-owo <3431391539@qq.com> Date: Sat, 1 Nov 2025 16:56:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=BArelationship=5Ffetcher=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0detached=E5=AF=B9=E8=B1=A1=E8=AE=BF=E9=97=AE=E4=BF=9D?= =?UTF-8?q?=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用getattr()和try-except安全访问relationship对象属性 - 防止缓存的detached对象导致Session绑定错误 - 即使字段访问失败也能继续执行,使用空字典 --- src/person_info/relationship_fetcher.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/person_info/relationship_fetcher.py b/src/person_info/relationship_fetcher.py index cd6be1df4..8942322db 100644 --- a/src/person_info/relationship_fetcher.py +++ b/src/person_info/relationship_fetcher.py @@ -196,12 +196,18 @@ class RelationshipFetcher: if relationship: # 将SQLAlchemy对象转换为字典以保持兼容性 - rel_data = { - "user_aliases": relationship.user_aliases, - "relationship_text": relationship.relationship_text, - "preference_keywords": relationship.preference_keywords, - "relationship_score": relationship.relationship_score, - } + # 使用 try-except 防止 detached 对象访问错误 + rel_data = {} + try: + rel_data = { + "user_aliases": getattr(relationship, "user_aliases", None), + "relationship_text": getattr(relationship, "relationship_text", None), + "preference_keywords": getattr(relationship, "preference_keywords", None), + "relationship_score": getattr(relationship, "relationship_score", None), + } + except Exception as attr_error: + logger.warning(f"访问relationship对象属性失败: {attr_error}") + rel_data = {} # 5.1 用户别名 if rel_data.get("user_aliases"):