No such table error
2025-06-022 turns11,376 charsgpt-4o
Summary
User is debugging a 500 Internal Server Error caused by a missing table in their SQLAlchemy-based API endpoint.
Messages
INFO: 127.0.0.1:54856 - "GET /api/generate_broadcast HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context
self.dialect.do_execute(
~~~~~~~~~~~~~~~~~~~~~~~^
cursor, str_statement, effective_parameters, context
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute
cursor.execute(statement, parameters)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: article_summaries
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.scope, self.receive, self.send
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 187, in __call__
raise exc
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 165, in __call__
await self.app(scope, receive, _send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/middleware/cors.py", line 93, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/middleware/cors.py", line 144, in simple_response
await self.app(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/fastapi/routing.py", line 301, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<3 lines>...
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/fastapi/routing.py", line 214, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/starlette/concurrency.py", line 37, in run_in_threadpool
return await anyio.to_thread.run_sync(func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/anyio/to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 2470, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 967, in run
result = context.run(func, *args)
File "/Users/danielkliewer/obj03/api/broadcast.py", line 20, in generate_broadcast
summaries = db.query(ArticleSummary.summary).all()
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/orm/query.py", line 2704, in all
return self._iter().all() # type: ignore
~~~~~~~~~~^^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/orm/query.py", line 2857, in _iter
result: Union[ScalarResult[_T], Result[_T]] = self.session.execute(
~~~~~~~~~~~~~~~~~~~~^
statement,
^^^^^^^^^^
params,
^^^^^^^
execution_options={"_sa_orm_load_options": self.load_options},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py", line 2365, in execute
return self._execute_internal(
~~~~~~~~~~~~~~~~~~~~~~^
statement,
^^^^^^^^^^
...<4 lines>...
_add_event=_add_event,
^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/orm/session.py", line 2251, in _execute_internal
result: Result[Any] = compile_state_cls.orm_execute_statement(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
self,
^^^^^
...<4 lines>...
conn,
^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/orm/context.py", line 306, in orm_execute_statement
result = conn.execute(
statement, params or {}, execution_options=execution_options
)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1415, in execute
return meth(
self,
distilled_parameters,
execution_options or NO_OPTIONS,
)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/sql/elements.py", line 523, in _execute_on_connection
return connection._execute_clauseelement(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
self, distilled_params, execution_options
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1637, in _execute_clauseelement
ret = self._execute_context(
dialect,
...<8 lines>...
cache_hit=cache_hit,
)
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
return self._exec_single_context(
~~~~~~~~~~~~~~~~~~~~~~~~~^
dialect, context, statement, parameters
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1982, in _exec_single_context
self._handle_dbapi_exception(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
e, str_statement, effective_parameters, cursor, context
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 2351, in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/base.py", line 1963, in _exec_single_context
self.dialect.do_execute(
~~~~~~~~~~~~~~~~~~~~~~~^
cursor, str_statement, effective_parameters, context
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/danielkliewer/obj03/venv/lib/python3.13/site-packages/sqlalchemy/engine/default.py", line 943, in do_execute
cursor.execute(statement, parameters)
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: article_summaries
[SQL: SELECT article_summaries.summary AS article_summaries_summary
FROM article_summaries]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
The error you're seeing:
```
sqlite3.OperationalError: no such table: article_summaries
```
means that your application is trying to query a table named `article_summaries`, but that table doesn't exist in your SQLite database.
---
### How to Fix It
You likely forgot to create or apply the database migrations. Here’s how to fix it, step by step:
#### ✅ If you are using **SQLAlchemy without Alembic**:
1. Make sure your `ArticleSummary` model is defined correctly.
2. Initialize the database by running something like:
```python
# somewhere in your main.py or a dedicated script
from your_model_file import Base
from your_database_file import engine # wherever your `engine = create_engine(...)` is
Base.metadata.create_all(bind=engine)
```
This will create all tables defined in your SQLAlchemy models.
---
#### ✅ If you are using **Alembic** for migrations:
1. Run:
```bash
alembic revision --autogenerate -m "create article summaries table"
```
2. Then apply the migration:
```bash
alembic upgrade head
```
---
### 🔁 Optional Debug Tips
- Ensure the correct database is being used (especially if you switched between SQLite and PostgreSQL).
- Print the DB path you're connecting to:
```python
print(engine.url) # will show file path for SQLite or connection string
```
- If using SQLite, delete the existing `.db` file and start fresh for local development (if no data needs to be preserved).
---
### 🔄 Summary Fix:
```python
# quick fix script (run once)
from your_model_file import Base
from your_database_file import engine
Base.metadata.create_all(bind=engine)
```
Let me know if you're using Alembic, Django ORM, or something else—I can tailor the fix.
gpt-4o