Prefetch with to_attr leaves the attribute missing when all forward relations are null
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 540
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 31
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing 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.
More from tortoise/tortoise-orm
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
tortoise/tortoise-orm#668 ·
Maintainers usually reply within 2 days
-
TimeField skips validators for timedelta valuesPossibly taken @IlyaKhalafi claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
tortoise/tortoise-orm#2404 ·
Maintainers usually reply within 2 days
-
Email domain allowlists reject equivalent mixed-case domainsPossibly taken @IlyaKhalafi claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
tortoise/tortoise-orm#2402 ·
Maintainers usually reply within 2 days
-
in_transaction() leaks a pool connection when begin() is cancelledPossibly taken @inchang-ing claimed this 3 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
tortoise/tortoise-orm#2391 ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 40/100
tortoise/tortoise-orm#2383 · 2 comments ·
Maintainers usually reply within 2 days
All issues in tortoise/tortoise-orm
Similar issues
-
fuzz fuzz-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
ready-for-agent
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
neilsh/stunt-reel#220 ·
Maintainers usually reply within 1 day
-
[Issue]: use_fp4_cache is accepted but ignored; the launcher always writes FP8 recordsPossibly taken @0z5a claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
area: cli priority: p3 size: S type: bug
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
finos/open-resource-broker#757 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 6 days