I've got a project made by FastApi with Tortoise ORM and aerich migrations. Aerich initialization was completed successfully, all migrations were done too. Here u can see my main.py:
app = FastAPI()
Tortoise.init_models(settings.TORTOISE_MODELS_LIST, "models")
register_tortoise(
app, config=settings.TORTOISE_ORM,
generate_schemas=True,
add_exception_handlers=True,
)
app.include_router(purchases.router.router)
if __name__ == "__main__":
uvicorn.run("main:app", debug=True, reload=True, lifespan='on')
Here is settings.py where TORTOISE_MODELS_LIST is placed:
TORTOISE_MODELS_LIST = ["purchases.models", "aerich.models"]
TORTOISE_ORM = {
"connections": {"default": DATABASE_URL},
"apps": {
"models": {
"models": TORTOISE_MODELS_LIST,
"default_connection": "default",
},
},
}
I've not problems on startup, but when I run this method:
@router.get("/get/latest", tags=["purchases"], response_model=List[Purchase_Pydantic],
description="Shows last 25 purchases")
async def get_latest_purchases():
purchases = await Purchase.all()[:25]
return Purchase_Pydantic.from_tortoise_orm(purchases)
I have such strange problem:
tortoise.exceptions.ConfigurationError: No DB associated to model
How to solve this issue? Aerich seems to be working cool...
For anyone see such bug: in models list place not a relative module name, but an absolute. So it will be 'app.items.models', and not 'items.models'. Works for me.