id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 29338 Can't use OuterRef in union Subquery Matthew Pava Mariusz Felisiak "When you make a QuerySet using the `union` method or the `|` operator, the QuerySet passed into the `union` method cannot reference `OuterRef` even when wrapped with `Subquery`. For example: {{{ cls = Document.objects.filter( checklist__isnull=False, part=OuterRef('id') ).values('checklist__customer', 'created') ots = Document.objects.filter( ownershiptransfer__isnull=False, part=OuterRef('id') ).values('ownershiptransfer__ship_target__contact', 'created') return self.annotate( owner=Subquery(cls.union(ots).values('owner')[:1]) ) }}} Returns this error: {{{ ValueError This queryset contains a reference to an outer query and may only be used in a subquery. }}} I get the same error with this statement: {{{ return self.annotate( owner=Subquery((cls | ots).values('owner')[:1]) ) }}} (As an aside, I also get an error when I try to apply an `order_by` clause.)" Bug closed Database layer (models, ORM) dev Normal fixed Jeff Can Sarıgöl InvalidInterrupt Accepted 1 0 0 0 0 0