diff --git a/src/connectors/mysql_connector.py b/src/connectors/mysql_connector.py index d51e3e1..0a9f6af 100644 --- a/src/connectors/mysql_connector.py +++ b/src/connectors/mysql_connector.py @@ -275,16 +275,16 @@ class MySQLConnector: if not rows: return - # Sort rows by NodeNum within the batch - # (rows already grouped by consolidation key from ORDER BY) - sorted_rows = sorted(rows, key=lambda r: int(r.get("NodeNum") or 0)) + # DO NOT re-sort by NodeNum! The database has already ordered by consolidation key, + # and re-sorting breaks the grouping. Rows with same key will not be consecutive anymore. + # We trust the MySQL ORDER BY to keep all rows of same key together. # Group rows by consolidation key - # Since rows are already ordered by key, all rows with same key are consecutive + # Since rows are already ordered by key from MySQL ORDER BY, all rows with same key are consecutive current_group = [] current_key = None - for row in sorted_rows: + for row in rows: key = ( row.get("UnitName"), row.get("ToolNameID"),