Rustango docs

Avoid N+1 queries with select_related

Problem

Rendering a list of posts with their author triggers one extra query per row.

Eager-load the related row in a single join with select_related, or batch a reverse/many relation with prefetch_related.

// One JOINed query instead of 1 + N:
let posts = Post::objects()
    .select_related("author")
    .order_by(&[("created_at", false)])
    .fetch(&pool)
    .await?;
for p in &posts {
    println!("{} by {}", p.title, p.author().name);
}

Many relations

Use prefetch_related("comments") for one-to-many / many-to-many — it runs a second batched IN (…) query and stitches the results in memory.