From 1d2628850ef92f8c517385eb7a3b9b6e4035a9fa Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 10 Nov 2024 00:30:33 +1100 Subject: [PATCH] Fix fencepost bug with TrialBalancer.from_cached --- drcr/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drcr/models.py b/drcr/models.py index 77d6504..36ceef6 100644 --- a/drcr/models.py +++ b/drcr/models.py @@ -241,12 +241,13 @@ class TrialBalancer: # First SELECT the last applicable dt by account # Then, among the transactions with that dt, SELECT the last applicable transaction_id # Then extract the running_balance for each account at that transaction_id + # NB: We need to specify DATE(...) otherwise SQLite appears to do a string comparison which doesn't work properly with SQLAlchemy's prepared statement? running_balances = db.session.execute(''' SELECT p3.account, running_balance FROM ( SELECT p1.account, max(p2.transaction_id) AS max_tid FROM ( - SELECT account, max(dt) AS max_dt FROM postings JOIN transactions ON postings.transaction_id = transactions.id WHERE dt < :start_date GROUP BY account + SELECT account, max(dt) AS max_dt FROM postings JOIN transactions ON postings.transaction_id = transactions.id WHERE DATE(dt) < DATE(:start_date) GROUP BY account ) p1 JOIN postings p2 ON p1.account = p2.account AND p1.max_dt = transactions.dt JOIN transactions ON p2.transaction_id = transactions.id GROUP BY p2.account ) p3 @@ -282,7 +283,7 @@ class TrialBalancer: ( SELECT p1.account, max(p2.transaction_id) AS max_tid FROM ( - SELECT account, max(dt) AS max_dt FROM postings JOIN transactions ON postings.transaction_id = transactions.id WHERE dt <= :end_date GROUP BY account + SELECT account, max(dt) AS max_dt FROM postings JOIN transactions ON postings.transaction_id = transactions.id WHERE DATE(dt) <= DATE(:end_date) GROUP BY account ) p1 JOIN postings p2 ON p1.account = p2.account AND p1.max_dt = transactions.dt JOIN transactions ON p2.transaction_id = transactions.id GROUP BY p2.account ) p3