34 lines
1.4 KiB
Python
34 lines
1.4 KiB
Python
from sqlalchemy import Integer, UniqueConstraint, ForeignKey
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from .base import Base, TimestampMixin, MYSQL_TABLE_ARGS
|
|
|
|
|
|
class UserRole(Base, TimestampMixin):
|
|
__tablename__ = "sys_user_role"
|
|
__table_args__ = (
|
|
UniqueConstraint("user_id", "role_id", name="uk_user_role"),
|
|
MYSQL_TABLE_ARGS,
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("sys_user.user_id"), nullable=False, index=True)
|
|
role_id: Mapped[int] = mapped_column(Integer, ForeignKey("sys_role.role_id"), nullable=False, index=True)
|
|
|
|
user = relationship("User", back_populates="roles")
|
|
role = relationship("Role", back_populates="users")
|
|
|
|
|
|
class RolePermission(Base, TimestampMixin):
|
|
__tablename__ = "sys_role_permission"
|
|
__table_args__ = (
|
|
UniqueConstraint("role_id", "perm_id", name="uk_role_perm"),
|
|
MYSQL_TABLE_ARGS,
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
role_id: Mapped[int] = mapped_column(Integer, ForeignKey("sys_role.role_id"), nullable=False, index=True)
|
|
perm_id: Mapped[int] = mapped_column(Integer, ForeignKey("sys_permission.perm_id", ondelete="CASCADE"), nullable=False, index=True)
|
|
|
|
role = relationship("Role", back_populates="permissions")
|
|
permission = relationship("Permission", back_populates="roles")
|