Skip to content

Roll back DB session in error handler to fix cascading InFailedSqlTransaction#2100

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-assets-page-error
Draft

Roll back DB session in error handler to fix cascading InFailedSqlTransaction#2100
Copilot wants to merge 3 commits intomainfrom
copilot/fix-assets-page-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 13, 2026

When a DB error (e.g. UndefinedColumn from a missing migration like account.attributes) aborts a PostgreSQL transaction mid-request, every subsequent query in that request fails with InFailedSqlTransaction — including the queries made by render_flexmeasures_template while building the error page. This caused the error page to fall back to bare render_template, losing all navigation/styling.

Changes

  • flexmeasures/utils/error_utils.py: Roll back the DB session in error_handling_router before rendering the HTML error response. The rollback is guarded with a try/except that logs a warning on failure rather than silently swallowing it.
  • documentation/changelog.rst: Add bugfix entry for v0.32.0.

Note on the root cause

The specific error reported (column account.attributes does not exist) is caused by not running flexmeasures db upgrade after pulling PR #2093, which added an attributes column to the account table via migration a3b1c2d4e5f6. The v0.32.0 changelog already includes the upgrade warning. This PR improves resilience so that any DB error in a view produces a properly rendered error page instead of a degraded fallback.

Original prompt

i am gettgint eh belwo error, when i try to load up my /assets page

[FLEXMEASURES][2026-04-13 12:05:06,021] INFO: 127.0.0.1 - - [13/Apr/2026 12:05:06] "GET /favicon.ico HTTP/1.1" 304 -
[FLEXMEASURES][2026-04-13 12:05:06,031] INFO: 127.0.0.1 - - [13/Apr/2026 12:05:06] "GET /favicon.ico HTTP/1.1" 304 -
[FLEXMEASURES][2026-04-13 13:43:58,728] ERROR: ProgrammingError - URL was: /assets - "(psycopg2.errors.UndefinedColumn) column account.attributes does not exist
LINE 1: ...dary_color, account.logo_url AS account_logo_url, account.at...
^

[SQL: SELECT account.id AS account_id, account.name AS account_name, account.primary_color AS account_primary_color, account.secondary_color AS account_secondary_color, account.logo_url AS account_logo_url, account.attributes AS account_attributes, account.consultancy_account_id AS account_consultancy_account_id
FROM account
WHERE account.id = %(pk_1)s]
[parameters: {'pk_1': 1}]
(Background on this error at: https://sqlalche.me/e/20/f405)" [occurred at /home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py (in do_execute, line 952)
Traceback (most recent call last):
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_classful.py", line 303, in proxy
response = view(**request.view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_classful.py", line 271, in inner
return fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/flask_login/utils.py", line 290, in decorated_view
return current_app.ensure_sync(func)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/flexmeasures/ui/views/assets/views.py", line 77, in index
user_can_create_assets=user_can_create_assets(),
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/flexmeasures/ui/views/assets/utils.py", line 27, in user_can_create_assets
account = current_user.account
^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 569, in get
return self.impl.get(state, dict_) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1096, in get
value = self._fire_loader_callables(state, key, passive)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1131, in fire_loader_callables
return self.callable
(state, passive)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 978, in _load_for_state
return self._emit_lazyload(
^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 1079, in _emit_lazyload
return loading.load_on_pk_identity(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 695, in load_on_pk_identity
session.execute(
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2351, in execute
return self._execute_internal(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/repos/seita_projects/flexmeasures/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2249, in _execute_internal
result: Result[Any] = compile_state_cls.orm_execute_statement(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/joshua-edward/Documents/...

Created from VS Code.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community bot commented Apr 13, 2026

Documentation build overview

📚 flexmeasures | 🛠️ Build #32235976 | 📁 Comparing 7766d2e against latest (8934f22)

  🔍 Preview build  

Show files changed (2 files in total): 📝 2 modified | ➕ 0 added | ➖ 0 deleted
File Status
changelog.html 📝 modified
api/v3_0.html 📝 modified

Copilot AI changed the title [WIP] Fix programming error on /assets page Roll back DB session in error handler to fix cascading InFailedSqlTransaction Apr 13, 2026
Copilot AI requested a review from joshuaunity April 13, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants