From acb2702d3e4df010538f3fe43305b910d9aa8570 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 10 Feb 2026 14:49:08 -0500 Subject: [PATCH] fix: track new transactions explicitly for post-import categorization session.new is emptied by auto-flush during duplicate detection queries, so we track new Transaction objects in a list instead. Co-Authored-By: Claude Opus 4.6 --- src/services/importer.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/services/importer.py b/src/services/importer.py index 92d0633..c9fc926 100644 --- a/src/services/importer.py +++ b/src/services/importer.py @@ -5,6 +5,7 @@ from sqlalchemy import func from sqlalchemy.orm import Session from src.models.transaction import Transaction +from src.services.categorizer import RuleBasedCategorizer from src.services.csv_reader import read_csv from src.services.normalizer import normalize_description @@ -23,6 +24,7 @@ class ImportService: rows = read_csv(file_path) imported = 0 duplicates = 0 + new_transactions: list[Transaction] = [] # Track how many times each key appears in the current batch so that # legitimate repeated transactions (same date/amount/description) in a @@ -65,8 +67,18 @@ class ImportService: source_category=source_cat, ) self.session.add(txn) + new_transactions.append(txn) imported += 1 + categorizer = RuleBasedCategorizer(self.session) + for txn in new_transactions: + if txn.category_id is None: + result = categorizer.categorize(txn) + if result: + txn.category_id = result.category_id + txn.tag = result.tag + txn.attributed_to_id = result.attributed_to_id + self.session.commit() return {"imported": imported, "duplicates": duplicates, "total_rows": len(rows)}