# SearchVectorField - joined reference not allowed for taggit field

**URL:** <https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106>\
**Category:** Using the ORM\
**Created:** [December 19, 2021, 10:23pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106 "2021-12-19T22:23:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zigzagdoom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zigzagdoom/32/5913_2.png) [@zigzagdoom](https://forum.djangoproject.com/u/zigzagdoom)\
**Post date:** [December 19, 2021, 10:23pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/1 "2021-12-19T22:23:11Z")

</div>

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

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [December 19, 2021, 11:23pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/2 "2021-12-19T23:23:34Z")

</div>

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`.

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

```

---

<div class="post-metadata">

**Author:** ![zigzagdoom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zigzagdoom/32/5913_2.png) [@zigzagdoom](https://forum.djangoproject.com/u/zigzagdoom)\
**Post date:** [December 19, 2021, 11:54pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/3 "2021-12-19T23:54:47Z")

</div>

> [@CodenameTim](#):
>
> ```auto
> 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`

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [December 20, 2021, 12:02am UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/4 "2021-12-20T00:02:26Z")

</div>

Hmm, maybe it was using a `Subquery`.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [December 20, 2021, 2:24pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/5 "2021-12-20T14:24:16Z")

</div>

@zigzagdoom I was able to confirm the way I handled it was with a [`Subquery`](https://docs.djangoproject.com/en/3.2/ref/models/expressions/#subquery-expressions) instance.

---

<div class="post-metadata">

**Author:** ![zigzagdoom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zigzagdoom/32/5913_2.png) [@zigzagdoom](https://forum.djangoproject.com/u/zigzagdoom)\
**Post date:** [December 21, 2021, 9:52pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/6 "2021-12-21T21:52:40Z")

</div>

Thanks a lot @CodenameTim , I will take a look

---

<div class="post-metadata">

**Author:** ![dlenskyi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dlenskyi/32/11015_2.png) [@dlenskyi](https://forum.djangoproject.com/u/dlenskyi)\
**Post date:** [November 23, 2022, 1:39pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/7 "2022-11-23T13:39:44Z")

</div>

@zigzagdoom were you able to solve this issue? I couldn’t make it work with a “Subquery” instance

---

<div class="post-metadata">

**Author:** ![codenameg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenameg/32/12021_2.png) [@codenameg](https://forum.djangoproject.com/u/codenameg)\
**Post date:** [February 2, 2023, 4:44pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/8 "2023-02-02T16:44:32Z")

</div>

@CodenameTim can you help with subquery for the above example.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 2, 2023, 4:53pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/9 "2023-02-02T16:53:59Z")

</div>

From [the docs example](https://docs.djangoproject.com/en/4.1/ref/models/expressions/#subquery-expressions) we have:

```python
from django.db.models import OuterRef, Subquery
newest = Comment.objects.filter(post=OuterRef('pk')).order_by('-created_at')
Post.objects.annotate(newest_commenter_email=Subquery(newest.values('email')[:1]))

```

We’re not going to use it exactly, but it’ll be something close.

Let’s say `Post.latest_activity` is a new datetime field we’ve added and we want to update all `Post` instances to reflect the latest created\_at from their comments.

```python
newest = Comment.objects.filter(post=OuterRef('pk')).order_by('-created_at')
Post.objects.annotate(
    latest_comment_datetime=Subquery(newest.values('created_at')[:1])
).update(
    latest_activity=F('latest_comment_datetime')
)

```

---

<div class="post-metadata">

**Author:** ![codenameg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenameg/32/12021_2.png) [@codenameg](https://forum.djangoproject.com/u/codenameg)\
**Post date:** [February 3, 2023, 2:19am UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/10 "2023-02-03T02:19:57Z")

</div>

thanks for the quick response. tried to apply the subquery to the above post from @zigzagdoom brings out another error

> [@](#):
>
> more than one row returned by a subquery used as an expression

since the tags is m2m, and a post can have multiple tags, restristicitng to one (using slice [:1]) doesn’t help.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 3, 2023, 10:34pm UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/11 "2023-02-03T22:34:12Z")

</div>

If you would like help with your problem please include more information about your situation. A good question will include what you’re trying to do, what you’re expecting to see and what you’re actually encountering. If it’s code related, please share the code as well. If there’s an error, include the error message and stacktrace.

---

<div class="post-metadata">

**Author:** ![codenameg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenameg/32/12021_2.png) [@codenameg](https://forum.djangoproject.com/u/codenameg)\
**Post date:** [February 4, 2023, 11:34am UTC](https://forum.djangoproject.com/t/searchvectorfield-joined-reference-not-allowed-for-taggit-field/11106/12 "2023-02-04T11:34:01Z")

</div>

@CodenameTim thanks, I solved the problem with subquery and StringAgg.

For others who are directed here from google for postgres full text search adding an m2m field or django-taggit package for tags, here is how i solved the error join not permitted error (similar to what @zigzagdoom got)

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

```

when trying this code

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

```

I used a StringAgg and used value expresions to add to the SearchVector, something like below pseudocode

```auto
Product.objects.all().annotate(tagnames=StringAgg("tags__name", delimiter=", ")).update(search_vector=SearchVector("title", "description") + SearchVector(Value("tagnames")))

```
