A dirty fix to prevent DetachedInstanceError when accessing the parent entity.

This commit is contained in:
Fabio Manganiello 2023-03-10 11:45:44 +01:00
parent ceb7a2f098
commit 3fcc9957d1
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,10 +1,11 @@
from typing import Dict, Iterable, List, Optional, Tuple from typing import Dict, Iterable, List, Optional, Tuple
from sqlalchemy.orm import Session from sqlalchemy.orm import Session, exc
from platypush.entities import Entity from platypush.entities import Entity
# pylint: disable=too-few-public-methods
class EntitiesMerger: class EntitiesMerger:
""" """
This object is in charge of detecting and merging entities that already This object is in charge of detecting and merging entities that already
@ -103,7 +104,12 @@ class EntitiesMerger:
the parent. the parent.
""" """
parent_id: Optional[int] = entity.parent_id 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 the entity has a parent with an ID, use that
if parent and parent.id: if parent and parent.id: