2026-06-09 23:30:05 +00:00
|
|
|
from datetime import datetime
|
2026-06-10 21:35:12 +00:00
|
|
|
|
|
|
|
|
from sqlalchemy import String, DateTime, Boolean, ForeignKey, func
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
2026-06-09 23:30:05 +00:00
|
|
|
from app.database import Base
|
|
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
|
2026-06-09 23:30:05 +00:00
|
|
|
class Workspace(Base):
|
|
|
|
|
__tablename__ = "workspaces"
|
|
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
|
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
|
|
|
description: Mapped[str] = mapped_column(String, nullable=True)
|
|
|
|
|
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
|
|
|
|
is_private: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())
|
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
|
|
|
DateTime, default=func.now(), onupdate=func.now()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Relationships
|
|
|
|
|
owner = relationship("User", back_populates="owned_workspaces")
|
|
|
|
|
members = relationship(
|
|
|
|
|
"WorkspaceMember", back_populates="workspace", cascade="all, delete-orphan"
|
|
|
|
|
)
|
|
|
|
|
tables = relationship(
|
|
|
|
|
"Table", back_populates="workspace", cascade="all, delete-orphan"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f"<Workspace(id={self.id}, name={self.name})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WorkspaceMember(Base):
|
|
|
|
|
__tablename__ = "workspace_members"
|
|
|
|
|
|
|
|
|
|
workspace_id: Mapped[int] = mapped_column(
|
|
|
|
|
ForeignKey("workspaces.id", ondelete="CASCADE"), primary_key=True
|
|
|
|
|
)
|
|
|
|
|
user_id: Mapped[int] = mapped_column(
|
|
|
|
|
ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
|
|
|
|
)
|
|
|
|
|
role: Mapped[str] = mapped_column(String(50), default="member")
|
|
|
|
|
joined_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())
|
|
|
|
|
|
|
|
|
|
# Relationships
|
|
|
|
|
workspace = relationship("Workspace", back_populates="members")
|
|
|
|
|
user = relationship("User", back_populates="workspace_memberships")
|
2026-06-09 23:30:05 +00:00
|
|
|
|
2026-06-10 21:35:12 +00:00
|
|
|
def __repr__(self):
|
|
|
|
|
return f"<WorkspaceMember(workspace_id={self.workspace_id}, user_id={self.user_id}, role={self.role})>"
|