Skip to content
Business Agile
← Work
Open source · Test data for asynchronous applications · Oct 2026

sqlmodel-async-factory

Test data factories that finally work against an asynchronous SQLAlchemy session.

Key figures

supported Python versions3.10+
lines of glue copied around, turned into a package20
licence, public package on PyPIMIT

`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.

What was done

  1. 01

    Published on PyPI under the MIT licence, documented on its own site

  2. 02

    The whole factory_boy declaration language kept

  3. 03

    The sub-factory case handled once: an asyncio task, awaited by the parent factory

  4. 04

    Fixes a silent failure: the test passes and the database stays empty

  5. 05

    Extracted from the test suite of a production application

Frequently asked questions

Why does factory_boy not work with an asynchronous session?

Because its SQLAlchemy factory calls `session.add()` and `session.commit()` synchronously. Against an `AsyncSession` those calls raise a `MissingGreenlet` error or never flush without saying so: the test passes and the database stays empty.

Do you have to rewrite your existing factories?

No. The whole declaration language is kept, `Sequence`, `LazyAttribute`, `Faker`, `SubFactory` and the lookup before creation. Only the create step becomes awaitable.

Why SQLModel rather than plain SQLAlchemy?

Because that is the stack I use in production and the lookups go through the SQLModel models. The need was real and repeated, not theoretical.

Other work

FabArto
fabarto.eu
Web
Ti Kosmeo
tikosmeo.fr
Web
Antiquité Mallarmé
antiquitemallarme.fr
Web
Maison Ozaé
maisonozae.eu
Web
ScienceProtect
scienceprotect.com
AI
Neontology
github.com/ontolocy/neontology
Open source
Pousses & Plantes
Plant marketplace
App
Arbre Agile
Artificial intelligence consulting and development
Web
Groupe BERREBI
Holding company and corporate website
Web
SNCF
Rail transport, internal applications
API
Pitchichi
guide.pitchichi.com
App
Appétit
Independent bookshop, cookbooks and photography
Web
Good Day
Media, consumer mobile application
App
Fondation Nestlé France
Corporate foundation, food and health
Web
DMD Prices
Art, canvas printing, online quoting
Web
IoTBox
Connected devices, industrial solutions
Web
PrevenFit
Services for fitness clubs, subscriber area
App
Securicate
Security equipment, online shop
E-commerce
WeChamp
Sport, corporate events
Web
Jaco
Media monitoring, subscription software
App
AWA
Education and film, project with an international reach
Web
Quizz Paris
Risk prevention, public awareness
App
La Gestion en Ligne
Rental management, online service
Web
Quotidien Libre
Online press, daily news
Web
Ophtalmologie Percy
Healthcare, military hospital
Web
Flashing Records
Music, label and event programming
Web
Apérothérapie
Wine and fine food, online retail
E-commerce
Arnaud CANIZARES
Contemporary art, artist portfolio
Web
Montebello Psychologue
Psychology and neuropsychology practice
Web
TapToSupport
Donation collection for non-profits
API
24hprojects
MVP delivery platform
App
Eco-Actives
Ecological transition and DIY workshops
E-commerce

A project? Let's talk this week.

A short brief, an answer within 48 hours, a firm estimate after scoping.

Describe my project