Skip to content
Advertisement

Using SQLAlchemy 1.4 classical / imperative mapping style?

I am using the clean architecture & TDD development method in my Python project. Updating from SQLAlchemy 1.3 to SQLAlchemy 1.4 broke the ability to test against an in-memory Postgres DB, and I can’t find how to fix the problem.

Following DDD principles, the project uses the new imperative mapping syntax which replace classical mapping declarations.

Here is a minimal (non)-working example, adapted from SQLAlchemy documentation: https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#orm-imperative-mapping

It requires installing and run PostgreSQL locally.

myapp/orm.py

JavaScript

myapp/model.py

JavaScript

tests/test_postgresql_inmemory.py

JavaScript

Result

JavaScript

Do you see what needs to be changed to make the test pass?

Advertisement

Answer

I could make the test pass by wrapping the mapper_registry.map_imperatively(…) in a start_mappers function, like I was doing before.

I initially thought that I had to replace this by configure_mappersSQLAlchemy documentation.

myapp/orm.py

JavaScript

tests/test_postgresql_inmemory.py

extract:

JavaScript

result

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement