Fix: avoid HAVING clause conflicts in custom field query filter on MariaDB

This commit is contained in:
stumpylog
2026-07-16 15:11:53 -07:00
parent 71557d7c64
commit d57721a41b
2 changed files with 9 additions and 2 deletions
+8 -1
View File
@@ -771,7 +771,14 @@ class CustomFieldQueryFilter(Filter):
)
q, annotations = parser.parse(value)
return qs.annotate(**annotations).filter(q)
# The Count(...) annotations above require a GROUP BY/HAVING to evaluate.
# Applying them directly to `qs` mixes that HAVING with `qs`'s existing
# joins (e.g. repeated tag joins from tags__id__all, the object-permission
# OR-filter), which some backends (e.g. MariaDB) fail to plan correctly,
# raising "Unknown column ... in 'HAVING'". Evaluating the annotation on
# an isolated queryset keeps the GROUP BY/HAVING self-contained.
matching_ids = Document.objects.annotate(**annotations).filter(q).values("pk")
return qs.filter(pk__in=matching_ids)
class DocumentFilterSet(FilterSet):
@@ -138,7 +138,7 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
title = str(kwargs)
document = Document.objects.create(
title=title,
checksum=title,
checksum=title[:64],
archive_serial_number=len(self.documents) + 1,
)
data = {