From 3fcc9957d1260582e7339de07bccfe38c36f77b3 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 10 Mar 2023 11:45:44 +0100 Subject: [PATCH] A dirty fix to prevent DetachedInstanceError when accessing the parent entity. --- platypush/entities/_engine/repo/merger.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/platypush/entities/_engine/repo/merger.py b/platypush/entities/_engine/repo/merger.py index 90b98714..eb8681a1 100644 --- a/platypush/entities/_engine/repo/merger.py +++ b/platypush/entities/_engine/repo/merger.py @@ -1,10 +1,11 @@ from typing import Dict, Iterable, List, Optional, Tuple -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, exc from platypush.entities import Entity +# pylint: disable=too-few-public-methods class EntitiesMerger: """ This object is in charge of detecting and merging entities that already @@ -103,7 +104,12 @@ class EntitiesMerger: the parent. """ parent_id: Optional[int] = entity.parent_id - parent: Optional[Entity] = entity.parent + try: + parent: Optional[Entity] = entity.parent + except exc.DetachedInstanceError: + # Dirty fix for `Parent instance <...> is not bound to a Session; + # lazy load operation of attribute 'parent' cannot proceed + parent = session.query(Entity).get(parent_id) if parent_id else None # If the entity has a parent with an ID, use that if parent and parent.id: