role_id and topic relations fixes

This commit is contained in:
2021-08-20 12:27:19 +03:00
parent ee3b186ba1
commit 3075dbb64b
19 changed files with 592 additions and 12 deletions

View File

@@ -2,14 +2,14 @@ import warnings
from typing import Type
from sqlalchemy import String, Column, ForeignKey, types, UniqueConstraint
from sqlalchemy import String, Integer, Column, ForeignKey, UniqueConstraint, TypeDecorator
from sqlalchemy.orm import relationship
from orm.base import Base, REGISTRY, engine, local_session
class ClassType(types.TypeDecorator):
impl = types.String
class ClassType(TypeDecorator):
impl = String
@property
def python_type(self):
@@ -33,7 +33,10 @@ class Organization(Base):
class Role(Base):
__tablename__ = 'role'
name: str = Column(String, nullable=False, unique=True, comment="Role Name")
id: int = Column(Integer, primary_key=True)
name: str = Column(String, nullable=False, comment="Role Name")
org_id: int = Column(ForeignKey("organization.id", ondelete="CASCADE"), nullable=False, comment="Organization")
permissions = relationship(lambda: Permission)

View File

@@ -1,10 +1,19 @@
from typing import List
from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, UniqueConstraint
from sqlalchemy.orm import relationship, backref
from orm import Permission
from orm.base import Base
Connection = Table('topic_connections',
Base.metadata,
Column('child', String, ForeignKey('topic.slug')),
Column('parent', String, ForeignKey('topic.slug')),
UniqueConstraint('parent', 'child', name='unique_usage')
)
class Topic(Base):
__tablename__ = 'topic'
@@ -13,6 +22,7 @@ class Topic(Base):
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")
alters = relationship(lambda: Topic, backref=backref("topic", remote_side=[slug]))
alter_id: str = Column(ForeignKey("topic.slug"))
# TODO: add all the fields
# 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)

View File

@@ -25,7 +25,7 @@ UserRatings = Table("user_ratings",
UserRoles = Table("user_roles",
Base.metadata,
Column('user_id', Integer, ForeignKey('user.id')),
Column('role', String, ForeignKey('role.name'))
Column('role_id', Integer, ForeignKey('role.id'))
)
class User(Base):