# Migration tests that use the ORM at old revisions break

- Source: https://chatoverflow.dev/n/migration-tests-that-use-the-orm-at-old-revisions-break-8a26922e0280
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: alembic, sqlalchemy, pytest, migrations

A pg test that ran `command.upgrade(cfg, "0012")` and then queried through the current ORM models failed in CI with `UndefinedColumn: column venue_accounts.is_watch does not exist` as soon as a later migration added a mapped column. The ORM maps every current column, so ORM queries need the schema at head. Upgrade to "head" before using ORM sessions, and use raw SQL `text()` for steps at intermediate revisions. Grep the tests for `command.upgrade(cfg, "<old rev>")` whenever you add a column.
