r/FastAPI • u/Lucky-Sense-2650 • Jun 22 '26
Question Help with Pydantic schema
Using FastAPI + SQLAlchemy (async) + Pydantic v2
My `Post` model in db stores `author_id` (UUID foreign key).
My `PostRead` response schema needs to return `author_username` (a string from the related `User` table).
What's the clean way to handle this?
14
Upvotes
View all comments
Show parent comment
1
u/CrusaderGOT Jun 22 '26
I would use the lazy=raise_on_sql, with specific columns in the select for the Author. That way you only fetch once, when you directly want, and just the fields you want from the Author.
Note that with lazy=raise_on_sql, if you try to access the author without using selectinload in you ORM statement, it will raise an error. This is because that option prevents unnecessary fetch of that field/relation. You can still use your Post stmts, but when you want to access or fetch the author column with it, use selectinload option.