2026-06-09 23:30:25 +00:00
|
|
|
from datetime import datetime
|
2026-06-10 21:35:12 +00:00
|
|
|
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
|
|
|
|
|
|
2026-06-09 23:30:25 +00:00
|
|
|
from app.database import Base
|
|
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
|
2026-06-09 23:30:25 +00:00
|
|
|
class Column(Base):
|
|
|
|
|
__tablename__ = "columns"
|
|
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
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())
|
2026-06-09 23:30:25 +00:00
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
# Relationships
|
2026-06-09 23:30:25 +00:00
|
|
|
table = relationship("Table", back_populates="columns")
|
2026-06-10 21:35:12 +00:00
|
|
|
cell_values = relationship(
|
|
|
|
|
"CellValue", back_populates="column", cascade="all, delete-orphan"
|
|
|
|
|
)
|
|
|
|
|
attachments = relationship("Attachment", back_populates="column")
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f"<Column(id={self.id}, name={self.name}, type={self.type})>"
|