Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初始化自带的几张auth只能用sqlmodel初始化??? #27

Open
Leewinner1 opened this issue Nov 4, 2023 · 1 comment
Open

初始化自带的几张auth只能用sqlmodel初始化??? #27

Leewinner1 opened this issue Nov 4, 2023 · 1 comment

Comments

@Leewinner1
Copy link

await site.db.async_run_sync(SQLModel.metadata.create_all, is_session=False)

sqlalchemy无法初始化自带的auth表,只能用sqlmodel

@jason810496
Copy link
Contributor

You can use Base.metadata.create_all from sqlalchemy to create it.
However, you need to bind and use the engine from AuthAdminSite.auth.db.

from contextlib import asynccontextmanager

from fastapi import FastAPI
from app.api.v1.routes import V1Router
from app.admin import admin_site # `AuthAdminSite` instance
from app.admin import admin_auth # `AuthAdminSite.auth` instance
from app.model.base import Base # `sqlachemy `2.0 `DeclarativeBase` 

# latest FastAPI version should use `asynccontextmanager` lifespan instead of `startup` event
# https://fastapi.tiangolo.com/advanced/events/
@asynccontextmanager
async def lifespan(app: FastAPI):
    # startup event todo
    # Note: **can't** use your `engine` instance, should use `AuthAdminSite.auth.db` instead
    Base.metadata.create_all(bind=admin_auth.db.engine) 
    await admin_auth.create_role_user("admin")
    yield
    # shutdown event todo

app = FastAPI(
    lifespan=lifespan,
)

app.include_router(V1Router)
admin_site.mount_app(app)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants