Uvicorn suddenly throwing 500 Internal Server error

8.4k Views Asked by At

Goinging through the Test-Driven Development with FastAPI and Docker course on testdriven dot io and I'm suddenly getting a 500 Internal Server error from Uvicorn. I have my code in git so I'm pretty sure there are no changes there that shouldn't be. Any help appreciated:

Here's the stack trace from the docker container:

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
2

There are 2 best solutions below

4
On BEST ANSWER

Should be a problem with your database schema. I suppose tortoise orm is trying to insert new data in your summary table but "textsummary" (field/relation to other table) does not exist. Try to check your schema and maybe make a migration.

If you build fresh docker container with db and app make sure you execute migration script during the container building

0
On

The following change will allow automatic creation of 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,
    )

This should solve the problems - generate_schemas=True was changed a couple of steps before, and probably this is the best way to go - they just did not write it that you need to revert to the previous version.