Skip to content

Conversation

agateblue
Copy link

This is just an attempt to deal with #68.

@agateblue
Copy link
Author

I took some time yesterday to track down the issue, and here is what I got:

  1. Apparently, prefetch_related on the queryset calls prefetch_related_objects (https://github.com/django/django/blob/1.10.1/django/db/models/query.py#L1387)
  2. This function calls prefetch_one_level (https://github.com/django/django/blob/1.10.1/django/db/models/query.py#L1473)
  3. At some point, this function retrieve a queryset from the prefetcher get_prefetch_queryset instance (https://github.com/django/django/blob/1.10.1/django/db/models/query.py#L1580). If I'm right, this queryset of related objects is then looped over and results are binded to original requested instances
  4. The mess seems to happen exactly here: to retrieve the related objects, an extra query is used by the get_prefetch_queryset method, and it is in this method I found the wrong PK's of the related objects to be returned.

I'll take more time to clearly identify if the line linked in 4 is where the error happens, because this ORM code is full of lambdas, descriptors and other deep-vodoo-black-magic stuff that are not easy to grasp. However, this extra query seems like a good culprit to me.

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