import uuid from sqlalchemy import BigInteger, ForeignKey, Index, String, Text, func, text from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column, relationship from app.database import Base class Document(Base): __tablename__ = "documents" __table_args__ = ( Index( "ix_documents_fts", text("to_tsvector('english', coalesce(extracted_text, ''))"), postgresql_using="gin", ), ) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True ) filename: Mapped[str] = mapped_column(String(255), nullable=False) original_filename: Mapped[str] = mapped_column(String(255), nullable=False) storage_path: Mapped[str] = mapped_column(Text, nullable=False) mime_type: Mapped[str] = mapped_column(String(100), nullable=False) file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) doc_type: Mapped[str] = mapped_column(String(50), nullable=False, default="other") extracted_text: Mapped[str | None] = mapped_column(Text, nullable=True) processing_status: Mapped[str] = mapped_column(String(20), nullable=False, default="pending") metadata_: Mapped[dict | None] = mapped_column("metadata", JSONB, nullable=True) user: Mapped["User"] = relationship(back_populates="documents") # noqa: F821