fix: 为relationship_fetcher添加detached对象访问保护
- 使用getattr()和try-except安全访问relationship对象属性 - 防止缓存的detached对象导致Session绑定错误 - 即使字段访问失败也能继续执行,使用空字典
This commit is contained in:
@@ -196,12 +196,18 @@ class RelationshipFetcher:
|
|||||||
|
|
||||||
if relationship:
|
if relationship:
|
||||||
# 将SQLAlchemy对象转换为字典以保持兼容性
|
# 将SQLAlchemy对象转换为字典以保持兼容性
|
||||||
rel_data = {
|
# 使用 try-except 防止 detached 对象访问错误
|
||||||
"user_aliases": relationship.user_aliases,
|
rel_data = {}
|
||||||
"relationship_text": relationship.relationship_text,
|
try:
|
||||||
"preference_keywords": relationship.preference_keywords,
|
rel_data = {
|
||||||
"relationship_score": relationship.relationship_score,
|
"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 用户别名
|
# 5.1 用户别名
|
||||||
if rel_data.get("user_aliases"):
|
if rel_data.get("user_aliases"):
|
||||||
|
|||||||
Reference in New Issue
Block a user