From 500532465342c0e20f030619c6f38d68771af642 Mon Sep 17 00:00:00 2001 From: OpenSquared Date: Tue, 23 Jun 2026 18:07:29 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20SQLite=20UNIQUE=20constraint=20on=20forw?= =?UTF-8?q?ard=5Fcurve=5Fdata=20=E2=80=94=20expressions=20not=20allowed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace UNIQUE(asset, DATE(fetched_at)) with dedicated fetch_date column + UNIQUE(asset, fetch_date). SQLite prohibits function calls in inline UNIQUE/PRIMARY KEY constraints. Co-Authored-By: Claude Sonnet 4.6 --- backend/services/database.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/services/database.py b/backend/services/database.py index ac3a336..364eae8 100644 --- a/backend/services/database.py +++ b/backend/services/database.py @@ -729,8 +729,9 @@ def init_db(): slope_pct REAL, structure TEXT NOT NULL DEFAULT 'unknown', months_spread INTEGER DEFAULT 3, + fetch_date TEXT NOT NULL DEFAULT (date('now')), fetched_at TEXT NOT NULL DEFAULT (datetime('now')), - UNIQUE(asset, DATE(fetched_at)) + UNIQUE(asset, fetch_date) )""") # Seed desk defaults (idempotent) @@ -4374,9 +4375,9 @@ def save_forward_curves(entries: List[Dict[str, Any]]) -> int: saved = 0 for e in entries: conn.execute("""INSERT INTO forward_curve_data - (asset, asset_class, front_price, far_price, slope_pct, structure, months_spread) - VALUES (?,?,?,?,?,?,?) - ON CONFLICT(asset, DATE(fetched_at)) DO UPDATE SET + (asset, asset_class, front_price, far_price, slope_pct, structure, months_spread, fetch_date) + VALUES (?,?,?,?,?,?,?,date('now')) + ON CONFLICT(asset, fetch_date) DO UPDATE SET front_price=excluded.front_price, far_price=excluded.far_price, slope_pct=excluded.slope_pct, structure=excluded.structure, fetched_at=datetime('now')""", @@ -4394,8 +4395,8 @@ def get_latest_forward_curves() -> List[Dict[str, Any]]: try: rows = conn.execute(""" SELECT * FROM forward_curve_data - WHERE (asset, DATE(fetched_at)) IN ( - SELECT asset, MAX(DATE(fetched_at)) FROM forward_curve_data GROUP BY asset + WHERE (asset, fetch_date) IN ( + SELECT asset, MAX(fetch_date) FROM forward_curve_data GROUP BY asset ) ORDER BY asset_class, asset""").fetchall() return [dict(r) for r in rows]