import uuid from datetime import datetime from sqlalchemy import BigInteger, CheckConstraint, ForeignKey, Text, func from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.db import Base class Message(Base): __tablename__ = "messages" __table_args__ = ( CheckConstraint("sender_type IN ('player', 'dm', 'system')", name="ck_message_sender_type"), ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) game_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("games.id", ondelete="CASCADE"), nullable=False, index=True ) sender_type: Mapped[str] = mapped_column(Text, nullable=False) user_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id"), nullable=True ) character_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("characters.id"), nullable=True ) content: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column( server_default=func.now(), nullable=False, index=True )