tortoise / tortoise/tortoise-orm
M2M Prefetch Loses Sub-Prefetches
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
During a M2M prefetch, any subsequent prefetches (particularly those in _prefetch_queries) are lost.
To Reproduce
I can provide a further set of models and example sometime, but I know the solution, so I'll just describe.
If you have a M2M relation and do a prefetch with it, then do a sub-prefetch, the M2M prefetch does not pass the subquery to the executor created.
Expected behavior
Prefetch should continue to prefetch until the DBA yells at me.
Additional context
Solution...
In base_executor.
In _prefetch_m2m_relation
Around line 395 insert prefetch_queries=related_query._prefetch_queries
await self.__class__(
model=related_query.model, db=self.db, prefetch_map=related_query._prefetch_map,
prefetch_queries=related_query._prefetch_queries
)._execute_prefetch_queries(related_object_list)
Without that line, a new executor is created without passing prefetch_queries, and the final prefetch query is lost.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read base_executor.py at _prefetch_m2m_relation and trace how the newly created executor receives the related query state. Verify the M2M prefetch path preserves nested prefetch queries, and confirm that sub-prefetches continue executing instead of being dropped.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100