tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.58k stars 378 forks source link

Tortoise ORM integration with FastAPI lifespan ('NoneType' object is not iterable) #1576

Closed daudln closed 4 months ago

daudln commented 6 months ago

Describe the bug I'm encountering an issue where app_lifespan doesn't seem to work correctly with Tortoise ORM. The issue appears to be related to a potential incompatibility in FastAPI (see Discussion fastapi/discussions/#11375 in the FastAPI repository). I've included more details about the problem in the FastAPI issue report.

To Reproduce

from contextlib import asynccontextmanager

import uvicorn
from faker import Faker
from fastapi import FastAPI
from pydantic import BaseModel
from tortoise import Model
from tortoise.contrib.fastapi import register_tortoise
from tortoise.fields import IntField, TextField

class User(Model):
    id = IntField(pk=True)
    name = TextField(null=False)
    email = TextField(null=False)

class UserIn(BaseModel):
    name: str
    email: str

class UserOut(UserIn):
    id: int

faker = Faker()

@asynccontextmanager
async def app_lifespan(app: FastAPI):
    # This lines (in for loop, tortoise logic) causes the error "coroutine 'app_lifespan' was never awaited"
    # Note: Without tortoise logic eg let say we print(i), no error occurs
    for i in range(100):
        await User.create(
            name=faker.name(),
            email=faker.email(),
        )
    yield

app = FastAPI(lifespan=app_lifespan)

register_tortoise(
    app,
    db_url="sqlite://data.sqlite3",
    modules={"models": [__name__]},
    generate_schemas=True,
)

if __name__ == "__main__":
    uvicorn.run("testapp:app", port=7000, reload=True)

Additional context

OS Ubuntu 23.10

Dependencies

vlakius commented 5 months ago

I can confirm that I am using fastapi version 0.110.0 with tortoise orm 0.20.0 and lifespan. The error NoneType' object is not iterableis due to the fact that the connection is not registered when you run the query

This is the solution I am currently using:

in my module: config_db.py

from contextlib import AbstractAsyncContextManager
from types import ModuleType
from typing import Dict, Iterable, Optional, Union

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from tortoise import Tortoise, connections
from tortoise.exceptions import DoesNotExist, IntegrityError

def register_tortoise(
    app: FastAPI,
    config: Optional[dict] = None,
    config_file: Optional[str] = None,
    db_url: Optional[str] = None,
    modules: Optional[Dict[str, Iterable[Union[str, ModuleType]]]] = None,
    generate_schemas: bool = False,
    add_exception_handlers: bool = False,
) -> AbstractAsyncContextManager:
    async def init_orm() -> None:  # pylint: disable=W0612
        await Tortoise.init(
            config=config, config_file=config_file, db_url=db_url, modules=modules
        )
        print(f"Tortoise-ORM started, {connections._get_storage()}")
        if generate_schemas:
            print("Tortoise-ORM generating schema")
            await Tortoise.generate_schemas()

    async def close_orm() -> None:  # pylint: disable=W0612
        await connections.close_all()
        print("Tortoise-ORM shutdown")

    class Manager(AbstractAsyncContextManager):
        async def __aenter__(self) -> "Manager":
            await init_orm()
            return self

        async def __aexit__(self, *args, **kwargs) -> None:
            await close_orm()

    if add_exception_handlers:

        @app.exception_handler(DoesNotExist)
        async def doesnotexist_exception_handler(request: Request, exc: DoesNotExist):
            return JSONResponse(status_code=404, content={"detail": str(exc)})

        @app.exception_handler(IntegrityError)
        async def integrityerror_exception_handler(
            request: Request, exc: IntegrityError
        ):
            return JSONResponse(
                status_code=422,
                content={
                    "detail": [{"loc": [], "msg": str(exc), "type": "IntegrityError"}]
                },
            )

    return Manager()

in main.py

from contextlib import asynccontextmanager
from fastapi import FastAPI, Request

from .config_db import register_tortoise

@asynccontextmanager
async def lifespan(app: FastAPI):
   print("===========  Start REST API ===========")
    async with register_tortoise(
        app,
        db_url="sqlite://data.sqlite3",
        modules={"models": [__name__]},
        generate_schemas=True,
        add_exception_handlers=True,
    ):
        # your starting logic here...

        yield 
        print("===========  Stop REST API ===========")
        # your stop logic here

app = FastAPI(lifespan=lifespan)
qingshuiyuyu commented 4 months ago

thanks ,i got it @vlakius

lyha23 commented 4 months ago

I have created a discussion about this question. https://github.com/tiangolo/fastapi/discussions/11507 From where I stand, the reason is lifespan will transform the function covered by @app.onevent("startup") . the consequence is that these functions will run as soon as app have initialed. BUT, these function might need run before some app-middleware applied, or need run after page logical function (such as this register_tortoise), SO I think fastapi should consider add some hooks to manage the life cycle.

daudln commented 4 months ago

Just a heads up, the PR #1541 addressing the Tortoise ORM v0.21.0 integration with FastAPI lifespan issue has been merged. 🎉 As a result, I'm going to go ahead and close the associated issue.