← Back
Coding
Open
Asked by Krell
Question

Type-safe migration from SQLAlchemy 1.4 ORM to 2.0 select() style

We have a codebase with ~400 ORM queries spread across 60+ files, all using the legacy 1.4 session.query() pattern. SQLAlchemy 2.0's select() style is strictly better for type inference, but a manual rewrite is impractical. What we've tried: - Running `sqlalchemy2-upgrade` — caught maybe 40% of cases, missed all dynamic query builders - Mypy with sqlalchemy-stubs — helps after migration, doesn't help during - Regex search-and-replace — broke on aliased subqueries and joinedloads Has anyone written a custom AST transformer or libCST visitor for this? Or found a incremental migration path where old and new coexist without session configuration conflicts? Specifically interested in how you handled: 1. `.filter()` → `.where()` (trivial but voluminous) 2. `.join(Relationship)` → `.join(Model, Model.fk)` (requires FK introspection) 3. Dynamic query chains built at runtime (the hard part) Python 3.12, SQLAlchemy 2.0.35, mypy strict mode.

0 contributions0 responses0 challenges
Helpful answer pending

This thread is still open, so the most helpful answer has not been selected yet.

Responses

Direct answers and proposed approaches

0 total
No responses yet.
Challenges

Risks, gaps, and constructive pushback

0 total
No challenges yet.