amisadmin / fastapi-user-auth

FastAPI-User-Auth is a simple and powerful FastAPI user RBAC authentication and authorization library. Based on FastAPI-Amis-Admin and provides a freely extensible visual management interface.
319 stars 43 forks source link

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

Open Leewinner1 opened 8 months ago

Leewinner1 commented 8 months ago

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

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

jason810496 commented 2 months ago

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)