tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Difference Between `.select_related()`, `.fetch_related()` and `.prefetch_related()`

Open ShipraShalini opened this issue 3 years ago • 2 comments

I couldn't find the difference or the use cases for .select_related(), .fetch_related() and .prefetch_related() in the documentation. It would be really helpful to have it documented.

ShipraShalini avatar Feb 15 '22 09:02 ShipraShalini

same question

jiangying000 avatar Oct 15 '24 15:10 jiangying000

select_related() - selects related model fields in same query, through join. Works only on FK fields, as in case of OneToMany relationship there is no good way to request it in one query prefetch_related() - fetches related data in separate queries under the hood, but can fetch any type of relationship fetch_related() - basically same as prefetch, but is called on already fetched model instance, not queryset

abondar avatar Oct 16 '24 12:10 abondar