from datetime import datetime from sqlalchemy import BigInteger, DateTime, ForeignKey, SmallInteger, String, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column, relationship from app.database import Base from app.models.base import BaseModel, SoftDeleteMixin class RoleUser(Base): __tablename__ = "bx_role_user" __table_args__ = (UniqueConstraint("role_id", "user_id"),) id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) role_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("bx_role.id"), nullable=False) user_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("bx_user.id"), nullable=False, index=True) create_by: Mapped[str | None] = mapped_column(String(64)) create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) class User(BaseModel, SoftDeleteMixin): __tablename__ = "bx_user" username: Mapped[str] = mapped_column(String(64), nullable=False, unique=True) password: Mapped[str] = mapped_column(String(128), nullable=False) real_name: Mapped[str | None] = mapped_column(String(64)) phone: Mapped[str | None] = mapped_column(String(20)) email: Mapped[str | None] = mapped_column(String(128)) avatar: Mapped[str | None] = mapped_column(String(256)) status: Mapped[int] = mapped_column(SmallInteger, nullable=False, default=1, server_default="1") remark: Mapped[str | None] = mapped_column(String(512)) roles: Mapped[list["Role"]] = relationship( "Role", secondary="bx_role_user", back_populates="users" )