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

Abierto
#2,374 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

@IlyaKhalafi ya está trabajando en esto.

Desde el 24/10/2026.

  • #2375 de @IlyaKhalafi — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
database

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
5.6k
Forks
540
Merge medio
4 d 10 h
PR fusionados (30 d)
31

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tortoise/tortoise-orm

Todos los issues de tortoise/tortoise-orm

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.