`factory_boy` is the standard tool for building test data in Python. It ships a SQLAlchemy factory, `SQLAlchemyModelFactory`, which calls `session.add()` then `session.commit()` synchronously. Against an `AsyncSession` those calls either raise `MissingGreenlet` or, worse, silently never flush. The test passes, the database is empty, and you go looking somewhere else.
The project in detail
This library fixes that without taking anything away. `AsyncSQLModelFactory` keeps the whole `factory_boy` declaration language, the `Sequence`, the `LazyAttribute`, the `Faker`, the `SubFactory` and the `sqlalchemy_get_or_create` lookup, and only makes the create step awaitable.
It came out of the test suite of a production FastAPI application whose data access is asynchronous end to end. Every attempt to reuse the stock SQLAlchemy factory ended in the same twenty lines of glue copied from one project to the next. The glue became a package, and the case that hurt most was handled once and properly: a sub-factory returns an `asyncio` task rather than an instance, so the parent factory has to await it before passing it to the model constructor. That is exactly what a developer in a hurry does not guess.
It targets SQLModel rather than plain SQLAlchemy, because that is the stack we use and the lookups go through its models. Its tests describe the expected behaviour, including the sub-factory case and the lookup before creation.
First published on PyPI on 28 October 2026, under the MIT licence, for Python 3.10 and above. Its documentation has its own site, built and published with the code it describes.