# Queryset.union() etc. vs. prefetch\_related()

**URL:** <https://forum.djangoproject.com/t/queryset-union-etc-vs-prefetch-related/10466>\
**Category:** ORM\
**Created:** [November 10, 2021, 10:41am UTC](https://forum.djangoproject.com/t/queryset-union-etc-vs-prefetch-related/10466 "2021-11-10T10:41:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [November 10, 2021, 10:41am UTC](https://forum.djangoproject.com/t/queryset-union-etc-vs-prefetch-related/10466/1 "2021-11-10T10:41:06Z")

</div>

Hello all,

The queryset `union()` docs say:

> only `LIMIT` , `OFFSET` , `COUNT(*)` , `ORDER BY` , and specifying columns (i.e. slicing, [`count()`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.count), [`exists()`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.exists), [`order_by()`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.order_by), and [`values()`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.values)/[`values_list()`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.values_list)) are allowed on the resulting `QuerySet` .

This makes sense, and is mostly caused by limitations imposed by SQL implementations – there’s not a lot of modifications you can add on such queries.

`prefetch_related()` is different – it adds separate queries, so SQL limitations do not really apply. The ORM still enforces it, though: If you call `prefetch_related()` on a union query, you get the standard exception:

> `django.db.utils.NotSupportedError: Calling QuerySet.prefetch_related() after union() is not supported.`

Somewhat surprisingly, if you call `prefetch_related()` before `union()`, it is applied (on the union queryset) – hinting that there is no good reason for the restriction.

Does anybody know a reason why we shouldn’t support `prefetch_related()` after `union()`?

(`prefetch_related()` on the argument querysets is silently ignored, that is, in `qs1.union(qs2)`, if `qs1` has `prefetch_related()` called on it, it is applied, but a `prefetch_related()` on `qs2` is ignored)

---

<div class="post-metadata">

**Author:** ![felixxm](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/felixxm/32/2479_2.png) [@felixxm](https://forum.djangoproject.com/u/felixxm)\
**Post date:** [November 12, 2021, 6:52am UTC](https://forum.djangoproject.com/t/queryset-union-etc-vs-prefetch-related/10466/2 "2021-11-12T06:52:41Z")

</div>

> [@shaib](#):
>
> Does anybody know a reason why we shouldn’t support `prefetch_related()` after `union()` ?

Not really. We have an accepted ticket [#28519](https://code.djangoproject.com/ticket/28519) for supporting other `QuerySet` methods on combined `QuerySets`, so a patch is welcome 🎁 .
