44from sqlalchemy .ext .declarative import DeclarativeMeta , declarative_base
55from sqlalchemy .orm import sessionmaker
66from 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)
911from 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
2038engine = create_async_engine (DATABASE_URL )
2139AsyncSessionLocal = sessionmaker (engine , class_ = AsyncSession , expire_on_commit = False )
@@ -31,3 +49,16 @@ async def create_db_and_tables():
3149async 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