question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug with prefetch_related

See original GitHub issue

Posted this few weeks ago to the mailinglist, but seems that it’s dead so re-posting here. (Maybe it would be a good idea to retire the mailinglist or post a notification there that it’s not been actively followed?)

So, I have a following model with tags:

tags = TaggableManager(through='knuckles.UUIDTaggedItem', blank=True)

where UUIDTaggedItem is:

class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):

    class Meta:
        verbose_name = "Tag"
        verbose_name_plural = "Tags"

When I query for the tags I get following:

In [18]: e = LiveEvent.objects.get(slug='dj-tony-004')
In [19]: e.tags.all()

Out[19]: <QuerySet [<Tag: 90s>, <Tag: dj tony>, <Tag: house>, <Tag: radioshow>, <Tag: 00's>, <Tag: trance>, <Tag: 90's>, <Tag: eurodance>]>


In [20]: e = LiveEvent.objects.prefetch_related('tags').get(slug='dj-tony-004')
In [21]: e.tags.all()

Out[21]: <QuerySet []>

So when using prefetch_related, I always get empty tags. Not sure if I’m doing something wrong here or if this is a bug?

I’m using Django version 1.11 and django-taggit version 0.22.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Uninencommented, Feb 20, 2020

Thanks a lot for the patch and the tests, @fumuumuf ! 👍

1reaction
fumuumufcommented, Feb 15, 2020

This occurs because instance_attr value isn’t match _prefetch_related_val. It worked fine by modifying as below. (only MySQL, SQLite3)

https://github.com/jazzband/django-taggit/blob/954d8360102fb2102db246bde1336a7e58ff10f2/taggit/managers.py#L114

            if issubclass(self.through, GenericUUIDTaggedItemBase):
                instance_attr = lambda obj: obj._get_pk_val().hex
            else:
                instance_attr = lambda obj: obj._get_pk_val()

            return (
                qs,
                attrgetter("_prefetch_related_val"),
                instance_attr,
                False,
                self.prefetch_cache_name,
                False,
            )
Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange prefetch_related Issue. Is this a bug? - Google Groups
It appears that the issue is because if the first item in the list is of a type "sms", then prefetch_related looks up...
Read more >
Prefetch_related failing against SQL Server - Stack Overflow
This bug occurs regularly for me under many different circumstances in this particular setup (always same Django version, driver, and DB), it's ...
Read more >
19607 (prefetch_related crash) - Django's bug tracker
The issue is hard to reproduce, but it seems to occur more when queryset has nested relations with more than one related object...
Read more >
GraphQL should use select_related and prefetch_related to ...
There is a bug in graphene_django that eliminates any usage of select_related or prefetch_related. This works around that limitation and allows the user...
Read more >
Five Common Django Mistakes - LAAC Technology
However, if we add a prefetch_related to the organization query, ... we've created a bug here where the system attempts to fetch the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found