Skip to content

Django 5.2 compatibility: prefetch related rely on TupleIn lookup - #513

Open
ddesvillechabrol wants to merge 3 commits into
Suor:masterfrom
ddesvillechabrol:composite_primary_key
Open

ddesvillechabrol wants to merge 3 commits into
Suor:masterfrom
ddesvillechabrol:composite_primary_key

Conversation

@ddesvillechabrol

@ddesvillechabrol ddesvillechabrol commented Sep 10, 2026 •

Copy link
Copy Markdown

Hello,
I experienced some issues with django 5.2 with prefetch related cache. Some prefetch related items in cache were not retrieved because the dnf part of conj keys was not set. Since django 5.2, the get_prefetch_queryset relies on Tuple Lookup since this commit django/django@626d77e .

The tuple lookup is used by composite primary keys newly introduced in django 5.2, sorry I don't have time to add every tuple lookup. So, I only added the TupleIn lookup that is used by prefetch related and I added a test with a composite primary key to check that TupleIn is taken into account by dnfs.

After the review, the composite key's conj key is set like conj:tests_orderlineitem:product_id=1&order_id=A75H
Prefetched items with unique keys keep the same conj ie: conj:test_order:order_id=A75H

Thanks !
Dimitri

@ddesvillechabrol
ddesvillechabrol force-pushed the composite_primary_key branch 3 times, most recently from 33e516e to af8ca5d Compare September 10, 2026 12:46
Comment thread cacheops/tree.py Outdated
{
(
where.lhs.alias,
attnames,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much suspect this don't be invalidated properly. Need to simulate conj for these, i.e a == 1 & b == 2

@ddesvillechabrol ddesvillechabrol Sep 10, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you were right, the invalidation doesn't work. I tried something that work but the information is duplicated. Maybe I should take into account that the children is a tuple lookup to be able to transform it as an AND condition to have the conj like you said a=1&b=2. I will check that next monday I think

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello ! I found some time today. The TupleIn conj is like the AND conj and it's well invalidated if the instance is updated 😄
Tell me if you see any other issues 🙏

Comment thread cacheops/tree.py Outdated
from django.db.models.sql.where import NothingNode
from django.db.models.lookups import Lookup, Exact, In, IsNull
from django.db.models.expressions import BaseExpression, Exists
from funcy import cat, group_by, join_with, lcat, lmap

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no non-semantic changes

Comment thread tests/models.py Outdated


class OrderLineItem(models.Model):
pk = models.CompositePrimaryKey("product_id", "order_id")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use if to make it work (be skipped) in older versions

@Suor

Suor commented Sep 10, 2026

Copy link
Copy Markdown
Owner

Hello, thanks for the PR. Can you please attend to the comments above. Also I see that tests are failing

since django 5.2 with the CompositePrimaryKey, prefetch related seems to
use TupleIn and ColPairs to get prefetch querysets and dnf key part used
in conj is lost
@ddesvillechabrol

Copy link
Copy Markdown
Author

Yes I use feature only available for django 5.2+ so all test with django version before 5.2 failed and sorry for the auto formating 🙏
I will check for invalidation with TupleIn. Thanks for your reactivity !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants