SQLAlchemy host pagination leaves recently updated targets on older pages
Bug Description
The SQLAlchemy backend's last_host_logs() and search_logs() functions can return targets in an incorrect order when a previously scanned target receives a newer event.
The queries group records by HostsLog.target but order by the non-aggregated HostsLog.id. As a result, a target with the newest scan event can remain on an older page.
Steps to Reproduce
- Use Nettacker commit
02275b6bwith Python 3.12 and SQLAlchemy 2.0.48. - Create an in-memory SQLite database using the existing
HostsLogmodel. - Insert 12 distinct targets (
host-0throughhost-11). - Insert an additional, newer event for
host-0. - Query pages 1 and 2 using
last_host_logs()andsearch_logs().
Expected Behavior
Targets should be ordered by their most recent matching event.
Page 1 should start with host-0, followed by host-11, host-10, and so on.
Actual Behavior
Both functions return host-0 on page 2, despite it having the newest event.
The observed results were:
Page 1: host-11, host-10, ..., host-2
Page 2: host-1, host-0
Root Cause
The SQLAlchemy queries use the following pattern:
.group_by(HostsLog.target)
.order_by(HostsLog.id.desc())
Ordering grouped records by a non-aggregated ID does not guarantee ordering by the newest record within each target group.
A possible fix is to order grouped targets using:
func.max(HostsLog.id).desc()
The query should apply pagination after grouping and ordering, while preserving existing search filters and response structures.
Environment
- OS: Windows 11
- Python: 3.12.14
- Nettacker: 0.4.2
- Commit:
02275b6b - SQLAlchemy: 2.0.48
- SQLite: 3.53.1
- Backend: SQLAlchemy SQLite (
use_apsw_for_sqlite=False)
Verification
The behavior was reproduced using synthetic records in an isolated in-memory SQLite database.
- Both affected SQLAlchemy functions failed the expected-order tests.
- Equivalent control queries returned the expected ordering.
- 67 existing focused tests passed.
- No external network targets were scanned.
The default APSW backend was not demonstrated to have this issue.
Related Work
I checked existing pagination and database discussions, including:
- #1294 – Pagination-related changes
- #1076 – APSW migration
- #1145 – Database writer queue
I did not find an existing report covering this specific grouped-ordering behavior within the issues and PR discussions reviewed.
Contribution
Could a maintainer confirm whether ordering targets by their newest matching event is the intended behavior?
If this is a valid issue and is available, I would be happy to work on a focused fix and add regression tests after approval and assignment.
Thank you!
hi @securestep9, can i take this one? the group_by(HostsLog.target) with order_by(HostsLog.id.desc()) in last_host_logs() and search_logs() is what puts host-0 on page 2.
plan is to order by func.max(HostsLog.id).desc() instead, keep pagination after the grouping and the search filters as they are, and add a test with the 12 targets + newer host-0 event from the repro. will start right away if assigned