Skip to content

Fix SQLAlchemy auto-correlation bug in vestigingen reader + add reader tests#21

Merged
rverk merged 1 commit intomainfrom
fix/fix-exists
Mar 12, 2026
Merged

Fix SQLAlchemy auto-correlation bug in vestigingen reader + add reader tests#21
rverk merged 1 commit intomainfrom
fix/fix-exists

Conversation

@rverk
Copy link
Collaborator

@rverk rverk commented Mar 12, 2026

Summary

  • Fix runtime crash in get_outdated_vestigingen_count() and get_outdated_vestigingen(): the exists() subquery referenced VestigingenORM, which also appears in the outer query JOIN — SQLAlchemy auto-correlated it by dropping VestigingenORM from the subquery's FROM clause. Fixed with .correlate(BasisProfielORM).
  • Add tests/db/test_kvkvestigingen_reader.py (18 tests) and tests/db/test_vestigingsprofiel_reader.py (21 tests) — integration tests that execute queries against SQLite, which would have caught this bug before merge.

Root cause

# Both the outer query and the subquery reference VestigingenORM.
# SQLAlchemy removes it from the subquery's FROM clause → empty FROM → crash.
blocked = (
    exists()
    .where(VestigingenORM.kvk_nummer == BasisProfielORM.kvk_nummer)
    ...
    .correlate(BasisProfielORM)  # ← fix: only correlate with BasisProfielORM
)

No database migration needed

No schema changes — only query logic and new tests.

Test plan

  • just check-all passes (run twice)
  • Vestigingen app cycles without the "returned no FROM clauses" error
  • KVK nummers with IPD0005/IPD1002 still blocked correctly in subsequent cycles

🤖 Generated with Claude Code

…ader tests

The exists() subquery in get_outdated_vestigingen referenced VestigingenORM,
which also appears in the outer query JOIN. SQLAlchemy auto-correlated the
subquery by removing VestigingenORM from its FROM clause, causing a runtime
error. Fixed by adding .correlate(BasisProfielORM) to explicitly scope
correlation to only BasisProfielORM.

Added integration tests for KvKVestigingenReader and VestigingsProfielReader
that execute queries against SQLite, which would have caught this bug before merge.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rverk rverk merged commit 160b1e2 into main Mar 12, 2026
1 check passed
@rverk rverk deleted the fix/fix-exists branch March 12, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant