SearchVectorField - joined reference not allowed for taggit field

My model definition is as follows (a simplified model of the one being used in actual product. There are a coupe of other fields:

from django.db import models
from taggit.managers import TaggableManager

from django.contrib.postgres.search import SearchVectorField
from django.contrib.postgres.indexes import GinIndex

class Product(models.Model):
    tags = TaggableManager()
    search_vector = SearchVectorField(null=True)

    class Meta:
        indexes = [
            GinIndex(fields=['search_vector'])
        ]

I run make migrations and migrate commands. These work correctly.

I then run the following set of commands to build the index:

search_vector = SearchVector('tags__name')
Product.objects.all().update(search_vector=search_vector)

I end up getting the following error:

django.core.exceptions.FieldError: Joined field references are not permitted in this query

This is clearly caused by the tags__name field, but I am not sure how to solve it.

Can someone please explain what I would need to do in order to run the above commands correctly?

Thanks!

I think I’ve run across this before. I believe I’ve solved it using an annotate before. Try creating an annotated field, then using that in the update.

Product.objects.annotate(
    inline_search_vector=SearchVector('tags__name')
).update(search_vector=F('inline_search_vector'))

Thanks @CodenameTim ,

I still seem to be hitting upon the error when using that technique:

django.core.exceptions.FieldError: Joined field references are not permitted in this query

Hmm, maybe it was using a Subquery.

@zigzagdoom I was able to confirm the way I handled it was with a Subquery instance.

Thanks a lot @CodenameTim , I will take a look