Skip to content

Is create_admin supposed to create the SQLite and database tables? #1276

@badlydrawnrob

Description

@badlydrawnrob

I'm currently rewriting the piccolo agsi new setup for FastAPI. Do you still need the create_tables() or create_db_tables() function to run, as well as the create_admin function?

I've changed a few things:

app = FastAPI(
    routes=[
        Route("/", HomeEndpoint),
        Mount(
            "/admin/",
            create_admin(
                tables=APP_CONFIG.table_classes,
                # Required when running under HTTPS:
                # allowed_hosts=['my_site.com']
            ),
        ),
        Mount("/static/", StaticFiles(directory="static")),
    ],
    lifespan=lifespan,
)

Becomes (using the FastAPI packages rather than Starlette)

app = FastAPI()

admin = create_admin(tables=APP_CONFIG.table_classes) #! (3)
app.mount("/admin", admin) # (2)

app.mount("/static", StaticFiles(directory="static"), name="static")

And piccolo_app.py

from piccolo.conf.apps import AppConfig
from tasks.tables import Task


APP_CONFIG = AppConfig(
    app_name="tasks",
    table_classes=[Task],
    migrations_folder_path=None,
    migration_dependencies=[],
    commands=[]
)

I'm trying to remove anything I feel is unnecessary or using Python "magic" so perhaps I've inadvertently broken something. Running the below code as well as create_admin seems to work, but the piccolo agsi new FastAPI example seems to have no create tables function that I can see.

@asynccontextmanager
async def lifespan(app: FastAPI):
    await create_db_tables(Task, if_not_exists=True)
    yield

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions