from __future__ import annotations from sqlalchemy import String, DateTime, Text, ForeignKey, Integer, text from sqlalchemy.orm import Mapped, mapped_column, relationship from datetime import datetime from fastapi_demo.app.db.session import Base class Asset(Base): __tablename__ = "assets" id: Mapped[str] = mapped_column(String, primary_key=True, index=True) name: Mapped[str] = mapped_column(String, nullable=False) serial: Mapped[str | None] = mapped_column(String, nullable=True) status: Mapped[str] = mapped_column(String, nullable=False) revision: Mapped[int] = mapped_column( Integer, nullable=False, server_default=text("0"), ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False ) events: Mapped[list["AssetEvent"]] = relationship( "AssetEvent", back_populates="asset", cascade="all, delete-orphan", passive_deletes=True, ) class AssetEvent(Base): __tablename__ = "asset_events" id: Mapped[str] = mapped_column(String, primary_key=True, index=True) asset_id: Mapped[str] = mapped_column( String, ForeignKey("assets.id", ondelete="CASCADE"), index=True ) from_status: Mapped[str] = mapped_column(String, nullable=False) to_status: Mapped[str] = mapped_column(String, nullable=False) at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) note: Mapped[str | None] = mapped_column(Text, nullable=True) asset: Mapped["Asset"] = relationship("Asset", back_populates="events")