diff --git a/src/common/database/api/crud.py b/src/common/database/api/crud.py index b3b06e93e..e652072b5 100644 --- a/src/common/database/api/crud.py +++ b/src/common/database/api/crud.py @@ -113,10 +113,19 @@ class CRUDBase: result = await session.execute(stmt) instance = result.scalar_one_or_none() - # 写入缓存 - if instance is not None and use_cache: - cache = await get_cache() - await cache.set(cache_key, instance) + if instance is not None: + # 触发所有列的加载,避免 detached 后的延迟加载问题 + # 遍历所有列属性以确保它们被加载到内存中 + for column in self.model.__table__.columns: + try: + getattr(instance, column.name) + except Exception: + pass # 忽略访问错误 + + # 写入缓存 + if use_cache: + cache = await get_cache() + await cache.set(cache_key, instance) return instance @@ -166,6 +175,14 @@ class CRUDBase: result = await session.execute(stmt) instances = result.scalars().all() + # 触发所有实例的列加载,避免 detached 后的延迟加载问题 + for instance in instances: + for column in self.model.__table__.columns: + try: + getattr(instance, column.name) + except Exception: + pass # 忽略访问错误 + # 写入缓存 if use_cache: cache = await get_cache() diff --git a/src/person_info/person_info.py b/src/person_info/person_info.py index c6a60f5f9..f5b4818bf 100644 --- a/src/person_info/person_info.py +++ b/src/person_info/person_info.py @@ -563,10 +563,6 @@ class PersonInfoManager: logger.debug("get_value获取失败:person_id不能为空") return None - # 使用CRUD进行查询 - crud = CRUDBase(PersonInfo) - record = await crud.get_by(person_id=person_id) - model_fields = [column.name for column in PersonInfo.__table__.columns] if field_name not in model_fields: @@ -577,11 +573,21 @@ class PersonInfoManager: logger.debug(f"get_value查询失败:字段'{field_name}'未在SQLAlchemy模型和默认配置中定义。") return None + # 使用CRUD进行查询 + crud = CRUDBase(PersonInfo) + record = await crud.get_by(person_id=person_id) + if record: - value = getattr(record, field_name) - if value is not None: - return value - else: + # 在访问属性前确保对象已加载所有数据 + # 使用 try-except 捕获可能的延迟加载错误 + try: + value = getattr(record, field_name) + if value is not None: + return value + else: + return copy.deepcopy(person_info_default.get(field_name)) + except Exception as e: + logger.warning(f"访问字段 {field_name} 失败: {e}, 使用默认值") return copy.deepcopy(person_info_default.get(field_name)) else: return copy.deepcopy(person_info_default.get(field_name)) diff --git a/src/person_info/relationship_fetcher.py b/src/person_info/relationship_fetcher.py index 9091f020a..cd6be1df4 100644 --- a/src/person_info/relationship_fetcher.py +++ b/src/person_info/relationship_fetcher.py @@ -200,7 +200,7 @@ class RelationshipFetcher: "user_aliases": relationship.user_aliases, "relationship_text": relationship.relationship_text, "preference_keywords": relationship.preference_keywords, - "relationship_score": relationship.affinity, + "relationship_score": relationship.relationship_score, } # 5.1 用户别名