Skip to content

Commit fe58699

Browse files
committed
Create team and user_team table
1 parent c1572e1 commit fe58699

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

transformerlab/shared/models/user_model.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
55
from sqlalchemy.orm import sessionmaker
66
from fastapi_users.db import SQLAlchemyBaseUserTableUUID
7+
from sqlalchemy import Column, String, ForeignKey
8+
import uuid
79

810
# Replace with your actual database URL (e.g., PostgreSQL, SQLite)
911
from transformerlab.db.constants import DATABASE_FILE_NAME, DATABASE_URL
@@ -16,6 +18,22 @@ class User(SQLAlchemyBaseUserTableUUID, Base):
1618
pass # You can add custom fields here later, like 'first_name: str'
1719

1820

21+
# 2. Define Team Model
22+
class Team(Base):
23+
__tablename__ = "teams"
24+
25+
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
26+
name = Column(String, nullable=False)
27+
28+
29+
# 3. Define User-Team Association Model
30+
class UserTeam(Base):
31+
__tablename__ = "users_teams"
32+
33+
user_id = Column(String, ForeignKey("users.id"), primary_key=True)
34+
team_id = Column(String, ForeignKey("teams.id"), primary_key=True)
35+
36+
1937
# 2. Setup the Async Engine and Session
2038
engine = create_async_engine(DATABASE_URL)
2139
AsyncSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
@@ -31,3 +49,16 @@ async def create_db_and_tables():
3149
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
3250
async with AsyncSessionLocal() as session:
3351
yield session
52+
53+
54+
# 5. Create default team if not exists
55+
async def create_default_team(session: AsyncSession) -> Team:
56+
stmt = select(Team).where(Team.name == "Default Team")
57+
result = await session.execute(stmt)
58+
team = result.scalar_one_or_none()
59+
if not team:
60+
team = Team(name="Default Team")
61+
session.add(team)
62+
await session.commit()
63+
await session.refresh(team)
64+
return team

0 commit comments

Comments
 (0)