Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

prefetch_related() that uses a Prefetch with a queryset is not joining correctly #129

Closed
brki opened this issue Feb 3, 2017 · 2 comments
Labels

Comments

@brki
Copy link
Contributor

brki commented Feb 3, 2017

Here is a failing test that can be added to versions_tests.tests.test_models.PrefetchingHistoricTests:

    def test_foreign_key_prefetch_with_historic_version(self):
        self.modify_objects()
        historic_city = City.objects.as_of(self.time1).get(identity=self.c1.identity)

        # Test with a simple prefetch
        team = Team.objects.as_of(self.time1).filter(identity=self.t1.identity).prefetch_related('city')[0]
        self.assertIsNotNone(team.city)
        self.assertEquals(team.city.id, historic_city.id)

        # Test with a Prefetch object without a queryset.
        team = Team.objects.as_of(self.time1).filter(
            identity=self.t1.identity
        ).prefetch_related(Prefetch(
            'city',
        ))[0]
        self.assertIsNotNone(team.city)
        self.assertEquals(team.city.id, historic_city.id)

        # Test with a Prefetch object with a queryset.
        team = Team.objects.as_of(self.time1).filter(
            identity=self.t1.identity
        ).prefetch_related(Prefetch(
            'city',
            queryset=City.objects.as_of(self.time1)
        ))[0]
        # This is currently failing:
        self.assertIsNotNone(team.city)
        self.assertEquals(team.city.id, historic_city.id)

The second-to-last assertion fails.

Inspecting the generated query, I can see that it tries to select the related objects using ids. It should however select using identities and an as_of clause.

I suspect that the VersionedReverseSingleRelatedObjectDescriptor needs to have a custom get_prefetch_queryset() implementation.

It probably makes sense to merge the Django-1.9-compatibility code before fixing this.

@brki brki added the bug label Feb 3, 2017
brki added a commit to brki/cleanerversion that referenced this issue Feb 3, 2017
…h queryset)

This commit is for Django 1.8.x.

This should be easily adaptable to work with Django 1.9+, too:
See get_prefetch_queryset() in
django.db.models.field.related_descriptors.ForwardManyToOneDescriptor.
brki added a commit that referenced this issue Feb 3, 2017
This commit is for Django 1.8.x.

This should be easily adaptable to work with Django 1.9+, too:
See get_prefetch_queryset() in
django.db.models.field.related_descriptors.ForwardManyToOneDescriptor.
brki added a commit to brki/cleanerversion that referenced this issue Feb 6, 2017
…h queryset)

This commit is for Django 1.8.x.

This should be easily adaptable to work with Django 1.9+, too:
See get_prefetch_queryset() in
django.db.models.field.related_descriptors.ForwardManyToOneDescriptor.
brki added a commit that referenced this issue Feb 6, 2017
This commit is for Django 1.8.x.

This should be easily adaptable to work with Django 1.9+, too:
See get_prefetch_queryset() in
django.db.models.field.related_descriptors.ForwardManyToOneDescriptor.
brki added a commit that referenced this issue Feb 6, 2017
This commit is for Django 1.8.x.

This should be easily adaptable to work with Django 1.9+, too:
See get_prefetch_queryset() in
django.db.models.field.related_descriptors.ForwardManyToOneDescriptor.
maennel added a commit that referenced this issue Feb 9, 2017
Fix for #129 (prefetch_related foreign_key-related object with queryset)
@brki
Copy link
Contributor Author

brki commented Mar 2, 2017

TODO: create a new release.

@maennel
Copy link
Contributor

maennel commented Sep 18, 2017

Can be closed with PR #140

@maennel maennel closed this as completed Sep 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants