mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-07-17 01:14:55 +00:00
Fix: avoid HAVING clause conflicts in custom field query filter on MariaDB
This commit is contained in:
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user