Uvicorn 突然抛出 500 内部服务器错误

Uvicorn suddenly throwing 500 Internal Server error

正在学习使用 FastAPI 进行测试驱动开发和 Docker 关于测试驱动点 io 的课程,我突然收到来自 Uvicorn 的 500 内部服务器错误。我在 git 中有我的代码,所以我很确定那里没有不应该的更改。任何帮助表示赞赏:

这是来自 docker 容器的堆栈跟踪:

ERROR:uvicorn.error:Exception in ASGI application

Traceback (most recent call last):

File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi

result = await app(self.scope, self.receive, self.send)

File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__

return await self.app(scope, receive, send)

File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__

await super().__call__(scope, receive, send)

File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__

await self.middleware_stack(scope, receive, send)

File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__

raise exc from None

File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__

await self.app(scope, receive, _send)

File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__

raise exc from None

File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__

await self.app(scope, receive, sender)

File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__

await route.handle(scope, receive, send)

File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle

await self.app(scope, receive, send)

File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 52, in app

response = await func(request)

File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 201, in app

raw_response = await run_endpoint_function(

File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 148, in run_endpoint_function

return await dependant.call(**values)

File "./app/api/summaries.py", line 22, in create_summary

summary_id = await crud.post(payload)

File "./app/api/crud.py", line 15, in post

await summary.save()

File "/usr/local/lib/python3.9/site-packages/tortoise/models.py", line 923, in save

await executor.execute_insert(self)

File "/usr/local/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 203, in execute_insert

insert_result = await self.db.execute_insert(self.insert_query, values)

File "/usr/local/lib/python3.9/site-packages/tortoise/backends/asyncpg/client.py", line 38, in translate_exceptions_

raise OperationalError(exc)

tortoise.exceptions.OperationalError: relation "textsummary" does not exist

应该是你的数据库模式有问题。我想 tortoise orm 正试图在您的摘要 table 中插入新数据,但是“textsummary”(field/relation 到其他 table)不存在。尝试检查您的模式并可能进行迁移。

如果您使用 db 和 app 构建新的 docker 容器,请确保在容器构建期间执行迁移脚本

以下更改将允许自动创建 data-tables。

def init_db(app: FastAPI) -> None:
    register_tortoise(
        app,
        db_url=os.environ.get("DATABASE_URL"),
        modules={"models": ["app.models.tortoise"]},
        generate_schemas=True,
        add_exception_handlers=True,
    )

这应该可以解决问题 - generate_schemas=True 之前更改了几个步骤,这可能是最好的方法 - 他们只是没有写你需要恢复到以前的版本。