Nothing loads unless you ask
#![allow(unused)]
fn main() {
let users = User::find().all(&pool).await?;
assert!(users[0].posts.is_empty()); // always empty here
}
Relations are not loaded by default, and there is no lazy loading either —
touching .posts will never trigger a query. The field just holds its default.
The rule that makes empty values meaningful
| You called | posts: [] means | profile: None means |
|---|---|---|
.find().all() | nothing — not loaded, unknown | nothing — not loaded, unknown |
.find().with(...).all() | this row really has zero children | this row really has no related row |
So .with() doesn’t just fetch data — it makes the empty case trustworthy.
Here is a real response from .with_nested(User::posts, |q| q.with(Post::comments)):
{
"id": 1, "name": "Ali Khan",
"posts": [
{ "id": 1, "title": "Zyvor Guide",
"comments": [
{ "id": 1, "body": "Great article!" },
{ "id": 2, "body": "Very helpful." }
] },
{ "id": 2, "title": "Axum Integration",
"comments": [ { "id": 3, "body": "Nice integration guide." } ] }
]
}
And in the same payload, another user’s post:
{ "id": 3, "title": "Zyvor v0.1 Released", "comments": [] }
That [] is a real answer: post 3 has no comments. Because .with() ran,
you can trust it. Without .with(), you couldn’t.
Practical takeaway: only expose relation fields on endpoints that actually
call .with(). That’s exactly what the DTO pattern in the Axum guide does.