forked from platypush/platypush
Modelling of parent/children relationships on entity level
This commit is contained in:
parent
0e0c90f0f2
commit
0edd73690b
2 changed files with 26 additions and 2 deletions
|
@ -8,6 +8,7 @@ import pkgutil
|
|||
from sqlalchemy import (
|
||||
Boolean,
|
||||
Column,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
String,
|
||||
|
@ -16,7 +17,7 @@ from sqlalchemy import (
|
|||
UniqueConstraint,
|
||||
inspect as schema_inspect,
|
||||
)
|
||||
from sqlalchemy.orm import ColumnProperty
|
||||
from sqlalchemy.orm import ColumnProperty, Mapped, backref, relationship
|
||||
|
||||
from platypush.common.db import Base
|
||||
from platypush.message import JSONAble
|
||||
|
@ -38,6 +39,12 @@ class Entity(Base):
|
|||
description = Column(String)
|
||||
type = Column(String, nullable=False, index=True)
|
||||
plugin = Column(String, nullable=False)
|
||||
parent_id = Column(
|
||||
Integer,
|
||||
ForeignKey(f'{__tablename__}.id', ondelete='CASCADE'),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
data = Column(JSON, default=dict)
|
||||
meta = Column(JSON, default=dict)
|
||||
is_read_only = Column(Boolean, default=False)
|
||||
|
@ -50,6 +57,19 @@ class Entity(Base):
|
|||
DateTime(timezone=False), default=datetime.utcnow(), onupdate=datetime.utcnow()
|
||||
)
|
||||
|
||||
parent: Mapped['Entity'] = relationship(
|
||||
'Entity',
|
||||
remote_side=[id],
|
||||
uselist=False,
|
||||
lazy=True,
|
||||
backref=backref(
|
||||
'children',
|
||||
remote_side=[parent_id],
|
||||
uselist=True,
|
||||
cascade='all, delete-orphan',
|
||||
),
|
||||
)
|
||||
|
||||
UniqueConstraint(external_id, plugin)
|
||||
|
||||
__table_args__ = (
|
||||
|
|
|
@ -6,6 +6,7 @@ from time import time
|
|||
from typing import Iterable, List, Optional
|
||||
|
||||
from sqlalchemy import and_, or_
|
||||
from sqlalchemy.exc import InvalidRequestError
|
||||
from sqlalchemy.orm import Session, make_transient
|
||||
|
||||
from platypush.context import get_bus, get_plugin
|
||||
|
@ -258,7 +259,10 @@ class EntitiesEngine(Thread):
|
|||
session.commit()
|
||||
|
||||
for e in entities:
|
||||
try:
|
||||
session.expunge(e)
|
||||
except InvalidRequestError:
|
||||
pass
|
||||
|
||||
with self._entities_cache_lock:
|
||||
for entity in entities:
|
||||
|
|
Loading…
Reference in a new issue