pythontortoise-orm

Tortoise ORM causes OperationalError: relation does not exist


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

Solution

  • 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