fix: PostgreSQL date subtraction in historical value query
All checks were successful
Deploy Development / deploy (push) Successful in 51s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

**Error:**
function pg_catalog.extract(unknown, integer) does not exist
HINT: No function matches the given name and argument types.

**Problem:**
In PostgreSQL, date - date returns INTEGER (days), not INTERVAL.
EXTRACT(EPOCH FROM integer) fails because EPOCH expects timestamp/interval.

**Solution:**
Changed from:
  ORDER BY ABS(EXTRACT(EPOCH FROM (date - '2026-01-01')))

To:
  ORDER BY ABS(date - '2026-01-01'::date)

This directly uses the day difference (integer) for sorting,
which is exactly what we need to find the closest date.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-28 13:22:05 +01:00
parent 1c7b5e0653
commit 7ffa8f039b

View File

@ -713,7 +713,7 @@ def _get_historical_value_for_goal_type(conn, profile_id: str, goal_type: str, t
FROM {source_table}
WHERE profile_id = %s
AND {date_col} BETWEEN %s AND %s
ORDER BY ABS(EXTRACT(EPOCH FROM ({date_col} - %s::date)))
ORDER BY ABS({date_col} - %s::date)
LIMIT 1
""", (
profile_id,