core/orm/topic.py

30 lines
1.3 KiB
Python
Raw Normal View History

2021-08-20 08:08:32 +00:00
from typing import List
from datetime import datetime
2021-08-20 09:27:19 +00:00
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, UniqueConstraint
2021-08-20 08:08:32 +00:00
from sqlalchemy.orm import relationship, backref
from orm import Permission
from orm.base import Base
2021-08-20 09:27:19 +00:00
Connection = Table('topic_connections',
Base.metadata,
Column('child', String, ForeignKey('topic.slug')),
Column('parent', String, ForeignKey('topic.slug')),
UniqueConstraint('parent', 'child', name='unique_usage')
)
2021-08-20 08:08:32 +00:00
class Topic(Base):
__tablename__ = 'topic'
slug: str = Column(String, unique = True, nullable = False, primary_key=True)
org_id: str = Column(ForeignKey("organization.id"), nullable=False)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Author")
value: str = Column(String, nullable=False, comment="Value")
2021-08-20 09:27:19 +00:00
# list of Topics where the current node is the "other party" or "child"
parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True)
# list of Topics where the current node is the "parent"
children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent)
2021-08-20 09:30:52 +00:00