Open-source personal finance manager. Self-hosted, privacy-first.
fix(transactions): break list-order ties on id so pages are stable (#1109)
Carlos Ferrús Ferri382d33e1dbc9
diff --git a/backend/app/services/transaction_service.py b/backend/app/services/transaction_service.pyindex f3dc000a..7c1bbc75 100644--- a/backend/app/services/transaction_service.py+++ b/backend/app/services/transaction_service.py@@ -492,13 +492,20 @@ async def get_transactions(}chosen_col = sort_columns.get(sort_by) if sort_by else Noneif chosen_col is None:- # Default: by date desc, with created_at as tiebreaker.- query = base_query.order_by(default_order_col.desc(), Transaction.created_at.desc())+ # Default: by date desc, with created_at as tiebreaker. id comes last+ # because a bank sync inserts its whole batch in one database+ # transaction, so those rows share created_at; without a unique key+ # the order between pages is undefined and rows repeat or vanish.+ query = base_query.order_by(+ default_order_col.desc(), Transaction.created_at.desc(), Transaction.id.desc()+ )else:direction = (chosen_col.asc() if sort_dir == "asc" else chosen_col.desc())# Always tie-break on date desc + created_at desc so equal values# stay in a sensible order (e.g. multiple txs with the same amount).- query = base_query.order_by(direction, default_order_col.desc(), Transaction.created_at.desc())+ query = base_query.order_by(+ direction, default_order_col.desc(), Transaction.created_at.desc(), Transaction.id.desc()+ )if not skip_pagination:query = query.offset((page - 1) * limit).limit(limit)diff --git a/backend/tests/test_transaction_service_coverage.py b/backend/tests/test_transaction_service_coverage.pyindex 17282562..19f02ade 100644--- a/backend/tests/test_transaction_service_coverage.py+++ b/backend/tests/test_transaction_service_coverage.py@@ -401,6 +401,33 @@ async def test_get_transactions_sorting(session, test_user, test_workspace, acctassert len(td) == 3+async def test_get_transactions_pages_rows_sharing_created_at_in_a_stable_order(+ session, test_user, test_workspace, acct+):+ # A bank sync inserts its batch in one database transaction, so the rows+ # share date and created_at. Paging through them must return each row+ # exactly once, in the same order on every request.+ created_at = datetime(2025, 3, 1, 12, tzinfo=timezone.utc)+ ids = sorted(uuid.uuid4() for _ in range(7))+ for txn_id in ids:+ await _mk_txn(session, test_user, acct, id=txn_id, created_at=created_at)++ async def paged_ids(**sort):+ paged = []+ for page in range(1, 5):+ rows, total, _ = await get_transactions(+ session, test_workspace.id, test_user.id, account_id=acct.id,+ page=page, limit=2, **sort,+ )+ assert total == 7+ paged.extend(t.id for t in rows)+ return paged++ # Default order, and an explicit sort whose column ties on every row too.+ assert await paged_ids() == sorted(ids, reverse=True)+ assert await paged_ids(sort_by="amount") == sorted(ids, reverse=True)++# ---------------------------------------------------------------------------# Group-scope visibility (lines 110-112, 146-155)# ---------------------------------------------------------------------------