Fixing sqlalchemy.exc.MissingGreenlet in FastAPI and Async SQLAlchemy 2.0
dev.to·2d·
Discuss: DEV
Async Rust
Preview
Report Post

The Problem

You just migrated your FastAPI application to Async SQLAlchemy 2.0. You run your code, everything looks fine, and then bam. Your API crashes with this scary error message:

sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() function.

If you are seeing this, you are not alone. This is the #1 pitfall developers encounter when switching from synchronous to asynchronous database calls.

The Cause

This error happens when you try to access a Relationship attribute (like user.posts) that was not loaded in the original query.

In the old synchronous world, SQLAlchemy would "lazily" load this data for you. It would silently pause your code, run a quick SQL query to get the posts, and then continue.

In the Async world, …

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help