Skip to content

Conversation

@willardy101-ank
Copy link

Pull Request description

Problem

When clicking the Home page in FACT 4.2, users encounter a database error:
CAST(anon_1.jsonb_array_elements_1 AS JSONB)['time'] DESC

This occurs because SQLAlchemy 2.0 automatically adds CAST operations when using dictionary-style access on JSONB columns, resulting in invalid PostgreSQL syntax.

Root Cause

The original code used:

query = select(subquery).order_by(subquery.c.jsonb_array_elements['time'].desc())
SQLAlchemy translates this into a CAST operation on already-unpacked JSONB array elements, which PostgreSQL rejects.

Solution
Add explicit column label to the JSONB array elements
Use SQLAlchemy's text() function with PostgreSQL's native -> operator
This bypasses SQLAlchemy's automatic type conversion

Changes
Modified get_latest_comments() method in src/storage/db_interface_frontend.py
Added text import from sqlalchemy
Used .label('comment_data') for explicit column naming
Used text("comment_data -> 'time' DESC") for ordering

Testing
Verified that Home page loads without database errors
Confirmed that latest comments are displayed correctly
Tested with SQLAlchemy 2.0.30 and PostgreSQL

Additional Notes
This issue affects FACT 4.2 installations. Users experiencing this issue need to:
Apply this fix
Clear Python cache: find . -name "__pycache__" -exec rm -rf {} +
Restart uwsgi processes: pkill -9 -f uwsgi

Fix SQLAlchemy JSONB cast error in get_latest_comments

When accessing JSONB array elements with dictionary syntax, SQLAlchemy 2.0
automatically adds CAST operations that cause PostgreSQL syntax errors.

This fix:
1. Adds explicit column label using .label('comment_data')
2. Uses text() with PostgreSQL -> operator to avoid automatic CAST
3. Imports 'text' from sqlalchemy

Fixes the "Database error when trying to read from the database" error
when accessing the home page.

Error: CAST(anon_1.jsonb_array_elements_1 AS JSONB)['time'] is invalid
@jstucke
Copy link
Collaborator

jstucke commented Dec 1, 2025

Hi and thank you for your contribution. Weirdly enough, I cannot seem to reproduce the error. Which version of PostgreSQL are you using? Does the this test fail for you (without your fix)?

pytest src/test/integration/storage/test_db_interface_frontend.py::test_get_latest_comments

@codecov-commenter
Copy link

codecov-commenter commented Dec 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.97%. Comparing base (14e85ca) to head (b1ae636).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1428   +/-   ##
=======================================
  Coverage   91.97%   91.97%           
=======================================
  Files         372      372           
  Lines       21015    21016    +1     
=======================================
+ Hits        19328    19329    +1     
  Misses       1687     1687           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jstucke
Copy link
Collaborator

jstucke commented Dec 1, 2025

Is it possible that the PR tries to fix a bug that is still in the latest release 4.2 but not on the master branch where it was already fixed in #1389?

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.

3 participants