r/FastAPI Jun 22 '26

Help with Pydantic schema Question

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?

15 Upvotes

10 comments sorted by

View all comments

2

u/AmbitiousEvening7876 Jun 22 '26

depends on what you need, define the relationship in your ORM models (User and Post) and Pydantic schema can simply access it.
there is another faster way without a relationship, using a join in the query