init_db.py 924 B

123456789101112131415161718192021222324
  1. from sqlalchemy.orm import Session
  2. from app import crud, schemas
  3. from app.core.config import settings
  4. from app.db import base # noqa: F401
  5. # make sure all SQL Alchemy models are imported (app.db.base) before initializing DB
  6. # otherwise, SQL Alchemy might fail to initialize relationships properly
  7. # for more details: https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/28
  8. def init_db(db: Session) -> None:
  9. # Tables should be created with Alembic migrations
  10. # But if you don't want to use migrations, create
  11. # the tables un-commenting the next line
  12. # Base.metadata.create_all(bind=engine)
  13. user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER)
  14. if not user:
  15. user_in = schemas.UserCreate(
  16. email=settings.FIRST_SUPERUSER,
  17. password=settings.FIRST_SUPERUSER_PASSWORD,
  18. )
  19. user = crud.user.create(db, obj_in=user_in) # noqa: F841