from datetime import datetime from typing import Optional from sqlalchemy import String, DateTime, Boolean, ForeignKey, Integer, func from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy import JSON from app.database import Base class Column(Base): __tablename__ = "columns" id: Mapped[int] = mapped_column(primary_key=True) table_id: Mapped[int] = mapped_column( ForeignKey("tables.id", ondelete="CASCADE"), nullable=False, index=True ) name: Mapped[str] = mapped_column(String(255), nullable=False) type: Mapped[str] = mapped_column( String(50), nullable=False ) # text, number, date, select, etc. required: Mapped[bool] = mapped_column(Boolean, default=False) default_value: Mapped[Optional[str]] = mapped_column(String, nullable=True) position: Mapped[int] = mapped_column(Integer, default=0) options: Mapped[Optional[dict]] = mapped_column( JSON, nullable=True ) # For select type: {"options": ["a", "b"]} created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now()) # Relationships table = relationship("Table", back_populates="columns") cell_values = relationship( "CellValue", back_populates="column", cascade="all, delete-orphan" ) attachments = relationship("Attachment", back_populates="column") def __repr__(self): return f""