Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
♻️ Update init_db
  • Loading branch information
tiangolo committed Nov 24, 2023
commit 6a7b9006a8dc7b35490d55ce4b86ffb3f798c66c
21 changes: 11 additions & 10 deletions src/backend/app/app/db/init_db.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
from sqlalchemy.orm import Session
from sqlmodel import Session, select

from app import crud, schemas
from app import crud
from app.core.config import settings
from app.db import base # noqa: F401
from app.models import User, UserCreate # noqa: F401

# make sure all SQL Alchemy models are imported (app.db.base) before initializing DB
# otherwise, SQL Alchemy might fail to initialize relationships properly
# 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/tiangolo/full-stack-fastapi-postgresql/issues/28


def init_db(db: Session) -> None:
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 line
# Base.metadata.create_all(bind=engine)

user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER)
user = session.exec(
select(User).where(User.email == settings.FIRST_SUPERUSER)
).first()
if not user:
user_in = schemas.UserCreate(
user_in = UserCreate(
email=settings.FIRST_SUPERUSER,
password=settings.FIRST_SUPERUSER_PASSWORD,
is_superuser=True,
)
user = crud.user.create(db, obj_in=user_in) # noqa: F841
user = crud.create_user(session, user_create=user_in)