|
1 | 1 | # database.py |
2 | 2 | from typing import AsyncGenerator |
3 | 3 | from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine |
4 | | -from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base |
5 | 4 | from sqlalchemy.orm import sessionmaker |
6 | 5 | from fastapi_users.db import SQLAlchemyBaseUserTableUUID |
7 | | -from sqlalchemy import Column, String, ForeignKey, select |
8 | | -import uuid |
| 6 | +from sqlalchemy import select |
9 | 7 |
|
10 | 8 | # Replace with your actual database URL (e.g., PostgreSQL, SQLite) |
11 | 9 | from transformerlab.db.constants import DATABASE_URL |
12 | | - |
13 | | -Base: DeclarativeMeta = declarative_base() |
| 10 | +from .models import Base, Team |
14 | 11 |
|
15 | 12 |
|
16 | 13 | # 1. Define your User Model (inherits from a FastAPI Users base class) |
17 | 14 | class User(SQLAlchemyBaseUserTableUUID, Base): |
18 | 15 | pass # You can add custom fields here later, like 'first_name: str' |
19 | 16 |
|
20 | 17 |
|
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("user.id"), primary_key=True) |
34 | | - team_id = Column(String, ForeignKey("teams.id"), primary_key=True) |
35 | | - |
36 | | - |
37 | 18 | # 2. Setup the Async Engine and Session |
38 | 19 | engine = create_async_engine(DATABASE_URL) |
39 | 20 | AsyncSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) |
|
0 commit comments