rename incoming/outgoing relations on Entity model

This commit is contained in:
phernandez
2025-01-04 00:23:02 -06:00
parent 7328e925a0
commit c59bdbf90e
6 changed files with 28 additions and 28 deletions
@@ -66,7 +66,7 @@ class KnowledgeWriter:
)
# Format outgoing and incoming relations separately
if entity.to_relations or entity.from_relations:
if entity.incoming_relations or entity.outgoing_relations:
sections.extend(
[
"## Relations",
@@ -75,7 +75,7 @@ class KnowledgeWriter:
)
# Outgoing relations (entity is "from")
for rel in entity.from_relations:
for rel in entity.outgoing_relations:
sections.append(f"- {rel.relation_type} [[{rel.to_entity.name}]] ")
if metadata:
+5 -5
View File
@@ -60,13 +60,13 @@ class Entity(Base):
observations = relationship(
"Observation", back_populates="entity", cascade="all, delete-orphan"
)
from_relations = relationship(
outgoing_relations = relationship(
"Relation",
back_populates="from_entity",
foreign_keys="[Relation.from_id]",
cascade="all, delete-orphan",
)
to_relations = relationship(
incoming_relations = relationship(
"Relation",
back_populates="to_entity",
foreign_keys="[Relation.to_id]",
@@ -76,7 +76,7 @@ class Entity(Base):
@property
def relations(self):
return self.to_relations + self.from_relations
return self.incoming_relations + self.outgoing_relations
def __repr__(self) -> str:
return f"Entity(id={self.id}, name='{self.name}', type='{self.entity_type}')"
@@ -153,8 +153,8 @@ class Relation(Base):
)
# Relationships
from_entity = relationship("Entity", foreign_keys=[from_id], back_populates="from_relations")
to_entity = relationship("Entity", foreign_keys=[to_id], back_populates="to_relations")
from_entity = relationship("Entity", foreign_keys=[from_id], back_populates="outgoing_relations")
to_entity = relationship("Entity", foreign_keys=[to_id], back_populates="incoming_relations")
def __repr__(self) -> str:
return f"Relation(id={self.id}, from_id={self.from_id}, to_id={self.to_id}, type='{self.relation_type}')"
@@ -45,8 +45,8 @@ class EntityRepository(Repository[Entity]):
query = query.where(
or_(
Entity.entity_type == entity_type,
Entity.from_relations.any(Relation.to_entity.has(entity_type=entity_type)),
Entity.to_relations.any(Relation.from_entity.has(entity_type=entity_type))
Entity.outgoing_relations.any(Relation.to_entity.has(entity_type=entity_type)),
Entity.incoming_relations.any(Relation.from_entity.has(entity_type=entity_type))
)
)
else:
@@ -108,11 +108,11 @@ class EntityRepository(Repository[Entity]):
return [
selectinload(Entity.observations),
# Load from_relations and both entities for each relation
selectinload(Entity.from_relations).selectinload(Relation.from_entity),
selectinload(Entity.from_relations).selectinload(Relation.to_entity),
selectinload(Entity.outgoing_relations).selectinload(Relation.from_entity),
selectinload(Entity.outgoing_relations).selectinload(Relation.to_entity),
# Load to_relations and both entities for each relation
selectinload(Entity.to_relations).selectinload(Relation.from_entity),
selectinload(Entity.to_relations).selectinload(Relation.to_entity),
selectinload(Entity.incoming_relations).selectinload(Relation.from_entity),
selectinload(Entity.incoming_relations).selectinload(Relation.to_entity),
]
async def find_by_path_ids(self, path_ids: List[str]) -> Sequence[Entity]: