I want to be able to filter a queryset based on the pk's existence in an annotated array, like the title says.
Example is below. Please note this is a heavily simplified example.
User.objects.annotate(
faves=ArraySubquery(
Favourite.objects.filter(user=OuterRef("pk")).values("object_id")
)
).filter(faves__len__gt=1).filter(
Exists(
Availability.objects.filter(
facility_id__in=OuterRef("faves"), cancelled=False
)
)
)
However, when I try to do this I get the following error:
ProgrammingError: operator does not exist: integer = integer[]
LINE 1: ...U0 WHERE (NOT U0."cancelled" AND U0."facility_id" IN (ARRAY(...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
There's something about ArrayAgg or ArraySubquery that doesn't play nice with IN checks. Does anyone know a way around this?