Files
score/backend/app/core/db.py
2025-05-24 11:04:23 +02:00

35 lines
1.2 KiB
Python

from sqlmodel import Session, create_engine, select
from app.core.config import settings
from app.models.user import User, UserCreate
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
# make sure all SQLModel models are imported (app.models) before initializing DB
# otherwise, SQLModel might fail to initialize relationships properly
# for more details: https://github.com/fastapi/full-stack-fastapi-template/issues/28
def init_db(session: Session) -> None:
# Tables should be created with Alembic migrations
# But if you don't want to use migrations, create
# the tables un-commenting the next lines
from app.models.base import BaseSQLModel
# This works because the models are already imported and registered from app.models
BaseSQLModel.metadata.create_all(engine)
user = session.exec(
select(User).where(User.email == settings.FIRST_SUPERUSER)
).first()
if not user:
user_in = UserCreate(
email=settings.FIRST_SUPERUSER,
password=settings.FIRST_SUPERUSER_PASSWORD,
is_verified=True,
is_active=True,
)
user = User.create(session=session, create_obj=user_in)