file-receive-backend/mods/user/relation.py

23 lines
1.0 KiB
Python

from sqlalchemy.orm import Mapped, mapped_column, relationship, declarative_base
from sqlalchemy import ForeignKey, Table, Column, String, UniqueConstraint
from .common import Base
def make_link_table(base: declarative_base, left: str, right: str, table_name="") -> Table:
link_table = Table(
table_name or f"{left}__{right}",
base.metadata,
Column(f"{left}_id", ForeignKey(f"{left}.id", ondelete='CASCADE', onupdate='CASCADE'), primary_key=True),
Column(f"{right}_id", ForeignKey(f"{right}.id", ondelete='CASCADE', onupdate='CASCADE'), primary_key=True),
)
return link_table
user__department = make_link_table(Base, 'user', 'department')
user__manage_department = make_link_table(Base, 'user', 'department',"user__manage_department")
user__post = make_link_table(Base, 'user', 'post')
user__auth_rule = make_link_table(Base, 'user', 'auth_rule')
department__auth_rule = make_link_table(Base, 'department', 'auth_rule')
post__auth_rule = make_link_table(Base, 'post', 'auth_rule')