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

Aperta
#2,374 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@IlyaKhalafi ci sta già lavorando.

Dal 24/10/2026.

  • #2375 di @IlyaKhalafi — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
database

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
5.6k
Fork
540
Merge medio
4g 10h
PR unite (30g)
31

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tortoise/tortoise-orm

Tutte le issue di tortoise/tortoise-orm

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.