Skip to content

Gracefully handle attaching an issue to a test result that doesn't exist #524

@rpbritton

Description

@rpbritton
2026-01-16T16:26:53.058Z [test-observer-api] Traceback (most recent call last):
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/pg8000/legacy.py", line 254, in execute
2026-01-16T16:26:53.058Z [test-observer-api]     self._context = self._c.execute_unnamed(
2026-01-16T16:26:53.058Z [test-observer-api]                     ^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/pg8000/core.py", line 715, in execute_unnamed
2026-01-16T16:26:53.058Z [test-observer-api]     self.handle_messages(context)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/pg8000/core.py", line 835, in handle_messages
2026-01-16T16:26:53.058Z [test-observer-api]     raise context.error
2026-01-16T16:26:53.058Z [test-observer-api] pg8000.exceptions.DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '23503', 'M': 'insert or update on table "issue_test_result_attachment" violates foreign key constraint "issue_test_result_attachment_test_result_id_fkey"', 'D': 'Key (test_result_id)=(1296685) is not present in table "test_result".', 's': 'public', 't': 'issue_test_result_attachment', 'n': 'issue_test_result_attachment_test_result_id_fkey', 'F': 'ri_triggers.c', 'L': '2528', 'R': 'ri_ReportViolation'}
2026-01-16T16:26:53.058Z [test-observer-api] 
2026-01-16T16:26:53.058Z [test-observer-api] During handling of the above exception, another exception occurred:
2026-01-16T16:26:53.058Z [test-observer-api] 
2026-01-16T16:26:53.058Z [test-observer-api] Traceback (most recent call last):
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
2026-01-16T16:26:53.058Z [test-observer-api]     self.dialect.do_execute(
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 945, in do_execute
2026-01-16T16:26:53.058Z [test-observer-api]     cursor.execute(statement, parameters)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/pg8000/legacy.py", line 281, in execute
2026-01-16T16:26:53.058Z [test-observer-api]     raise cls(msg)
2026-01-16T16:26:53.058Z [test-observer-api] pg8000.dbapi.ProgrammingError: {'S': 'ERROR', 'V': 'ERROR', 'C': '23503', 'M': 'insert or update on table "issue_test_result_attachment" violates foreign key constraint "issue_test_result_attachment_test_result_id_fkey"', 'D': 'Key (test_result_id)=(1296685) is not present in table "test_result".', 's': 'public', 't': 'issue_test_result_attachment', 'n': 'issue_test_result_attachment_test_result_id_fkey', 'F': 'ri_triggers.c', 'L': '2528', 'R': 'ri_ReportViolation'}
2026-01-16T16:26:53.058Z [test-observer-api] 
2026-01-16T16:26:53.058Z [test-observer-api] The above exception was the direct cause of the following exception:
2026-01-16T16:26:53.058Z [test-observer-api] 
2026-01-16T16:26:53.058Z [test-observer-api] Traceback (most recent call last):
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 436, in run_asgi
2026-01-16T16:26:53.058Z [test-observer-api]     result = await app(  # type: ignore[func-returns-value]
2026-01-16T16:26:53.058Z [test-observer-api]              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     return await self.app(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await super().__call__(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await self.middleware_stack(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     raise exc
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await self.app(scope, receive, _send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/middleware/sessions.py", line 85, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await self.app(scope, receive, send_wrapper)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/middleware/cors.py", line 85, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await self.app(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
2026-01-16T16:26:53.058Z [test-observer-api]     raise exc
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
2026-01-16T16:26:53.058Z [test-observer-api]     await app(scope, receive, sender)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/routing.py", line 714, in __call__
2026-01-16T16:26:53.058Z [test-observer-api]     await self.middleware_stack(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/routing.py", line 734, in app
2026-01-16T16:26:53.058Z [test-observer-api]     await route.handle(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle
2026-01-16T16:26:53.058Z [test-observer-api]     await self.app(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app
2026-01-16T16:26:53.058Z [test-observer-api]     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
2026-01-16T16:26:53.058Z [test-observer-api]     raise exc
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
2026-01-16T16:26:53.058Z [test-observer-api]     await app(scope, receive, sender)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
2026-01-16T16:26:53.058Z [test-observer-api]     response = await f(request)
2026-01-16T16:26:53.058Z [test-observer-api]                ^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 301, in app
2026-01-16T16:26:53.058Z [test-observer-api]     raw_response = await run_endpoint_function(
2026-01-16T16:26:53.058Z [test-observer-api]                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/fastapi/routing.py", line 214, in run_endpoint_function
2026-01-16T16:26:53.058Z [test-observer-api]     return await run_in_threadpool(dependant.call, **values)
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/starlette/concurrency.py", line 37, in run_in_threadpool
2026-01-16T16:26:53.058Z [test-observer-api]     return await anyio.to_thread.run_sync(func)
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
2026-01-16T16:26:53.058Z [test-observer-api]     return await get_async_backend().run_sync_in_worker_thread(
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2470, in run_sync_in_worker_thread
2026-01-16T16:26:53.058Z [test-observer-api]     return await future
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 967, in run
2026-01-16T16:26:53.058Z [test-observer-api]     result = context.run(func, *args)
2026-01-16T16:26:53.058Z [test-observer-api]              ^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/test_observer/controllers/issues/issue_attachments.py", line 160, in add_issue_attachments
2026-01-16T16:26:53.058Z [test-observer-api]     return modify_issue_attachments(db, issue_id, request, detach=False)
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/test_observer/controllers/issues/issue_attachments.py", line 77, in modify_issue_attachments
2026-01-16T16:26:53.058Z [test-observer-api]     db.execute(
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2365, in execute
2026-01-16T16:26:53.058Z [test-observer-api]     return self._execute_internal(
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2251, in _execute_internal
2026-01-16T16:26:53.058Z [test-observer-api]     result: Result[Any] = compile_state_cls.orm_execute_statement(
2026-01-16T16:26:53.058Z [test-observer-api]                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/orm/bulk_persistence.py", line 1294, in orm_execute_statement
2026-01-16T16:26:53.058Z [test-observer-api]     result = conn.execute(
2026-01-16T16:26:53.058Z [test-observer-api]              ^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1416, in execute
2026-01-16T16:26:53.058Z [test-observer-api]     return meth(
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 523, in _execute_on_connection
2026-01-16T16:26:53.058Z [test-observer-api]     return connection._execute_clauseelement(
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
2026-01-16T16:26:53.058Z [test-observer-api]     ret = self._execute_context(
2026-01-16T16:26:53.058Z [test-observer-api]           ^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1843, in _execute_context
2026-01-16T16:26:53.058Z [test-observer-api]     return self._exec_single_context(
2026-01-16T16:26:53.058Z [test-observer-api]            ^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
2026-01-16T16:26:53.058Z [test-observer-api]     self._handle_dbapi_exception(
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2352, in _handle_dbapi_exception
2026-01-16T16:26:53.058Z [test-observer-api]     raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
2026-01-16T16:26:53.058Z [test-observer-api]     self.dialect.do_execute(
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 945, in do_execute
2026-01-16T16:26:53.058Z [test-observer-api]     cursor.execute(statement, parameters)
2026-01-16T16:26:53.058Z [test-observer-api]   File "/home/app/.venv/lib/python3.12/site-packages/pg8000/legacy.py", line 281, in execute
2026-01-16T16:26:53.058Z [test-observer-api]     raise cls(msg)
2026-01-16T16:26:53.058Z [test-observer-api] sqlalchemy.exc.ProgrammingError: (pg8000.dbapi.ProgrammingError) {'S': 'ERROR', 'V': 'ERROR', 'C': '23503', 'M': 'insert or update on table "issue_test_result_attachment" violates foreign key constraint "issue_test_result_attachment_test_result_id_fkey"', 'D': 'Key (test_result_id)=(1296685) is not present in table "test_result".', 's': 'public', 't': 'issue_test_result_attachment', 'n': 'issue_test_result_attachment_test_result_id_fkey', 'F': 'ri_triggers.c', 'L': '2528', 'R': 'ri_ReportViolation'}
2026-01-16T16:26:53.058Z [test-observer-api] [SQL: INSERT INTO issue_test_result_attachment (issue_id, test_result_id, attachment_rule_id, created_at, updated_at) VALUES (%s::INTEGER, %s::INTEGER, %s::INTEGER, now(), now()) ON CONFLICT DO NOTHING]
2026-01-16T16:26:53.058Z [test-observer-api] [parameters: (372, 1296685, None)]
2026-01-16T16:26:53.058Z [test-observer-api] (Background on this error at: https://sqlalche.me/e/20/f405)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions