Adding a custom Prefetch to a cloned queryset mutates the original query

オープン
#2,372 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@IlyaKhalafi がすでに取り組んでいます。

2026年10月24日 から。

  • #2373 @IlyaKhalafi による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
databases

調査の方向性

The repro imports Prefetch from tortoise.query_utils and calls prefetch_related. Read the prefetch_related implementation and the queryset clone logic. Run the snippet to confirm second_parent leaks into original. Done when original queryset remains unchanged; PR #2373 is open.

索引モデルが issue の本文から書いたものです。

説明

Here Child.parent is a foreign key to Parent. On current develop, a queryset clone shares the lists of custom prefetch queries with the original. Adding another Prefetch for the same relation changes both queries.

from tortoise.query_utils import Prefetch

original = Child.all().prefetch_related(
    Prefetch("parent", Parent.all(), to_attr="first_parent")
)
derived = original.prefetch_related(
    Prefetch("parent", Parent.all(), to_attr="second_parent")
)
row = await original.first()

When a child exists, row also has second_parent even though it was only requested on derived. The original queryset should remain unchanged.

主要言語
Python
スター
5.6k
フォーク
540
平均マージ
4日 4時間
マージ済み PR(30日)
32

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tortoise/tortoise-orm のほかの issue

tortoise/tortoise-orm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。