Prefetch with to_attr leaves the attribute missing when all forward relations are null

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

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

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

2026年10月24日 から。

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

評価

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

調査の方向性

The repro in the issue shows a nullable FK prefetched with Prefetch(..., to_attr=...) raising AttributeError instead of setting the attribute to None when all keys are null. Start in tortoise/query_utils.py and the prefetch population code in fields/relational or the queryset prefetch path, where to_attr results are assigned. Look for the branch handling empty/null-only relation groups. Done means the repro returns None; a linked pull request (#2375) already exists, so check it before starting.

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

説明

Here Child.parent is a nullable foreign key to Parent. The same behavior occurs with nullable one-to-one fields. On current develop, prefetching the relation with to_attr leaves the requested attribute missing when every related key is null.

from tortoise.query_utils import Prefetch

child = await Child.create(parent=None)
row = await Child.filter(pk=child.pk).prefetch_related(
    Prefetch("parent", Parent.all(), to_attr="cached_parent")
).get()
print(row.cached_parent)

The last line raises AttributeError. It should return None, consistent with prefetching a mixed group that includes a non-null relation.

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

環境構築

はじめの一歩

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

tortoise/tortoise-orm のほかの issue

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

似ている issue

Python の issue をもっと見る

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

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