Skip to content

feat Eager loading / prefetch_related #9

@Ox54

Description

@Ox54

Summary

Support eager loading of relationships to avoid N+1 query patterns. Users should be able to prefetch related models in a single (or batched) query instead of loading relations one-by-one when iterating.

Current state

Status: Not implemented.

Documentation references:

  • docs/coming-soon.md (Eager Loading / Prefetch Related)
  • docs/guide/queries.md (lines 211-214, 318-321)

Current workaround: Manually load relationships as needed; be mindful of N+1 patterns.

Proposed API (example)

# This does not work yet
posts = await Post.select().prefetch_related("author").all()
  • Query builder would gain something like .prefetch_related("author") (and possibly multiple relations, e.g. .prefetch_related("author", "comments")).
  • Resulting instances would have the requested relations already loaded (e.g. post.author usable without an extra query).

Acceptance criteria

  • Query API supports prefetching one or more relation names (e.g. prefetch_related("author") or prefetch_related("author", "comments")).
  • Prefetched relations are loaded efficiently (batched / minimal queries), not per-row.
  • Accessing a prefetched relation on a model instance does not trigger an additional query.
  • Docs updated: remove from Coming Soon and document in the queries guide; update any performance/concepts docs that reference prefetch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions