From 151fc37fbd49d8a171c401b28ca7b229dc0b636f Mon Sep 17 00:00:00 2001 From: AarDG10 Date: Thu, 15 Jan 2026 18:50:28 +0530 Subject: [PATCH] fix(validation): maintain compatibility with different way of writing queries --- frappe/database/query.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/frappe/database/query.py b/frappe/database/query.py index 8118326c20..0628286432 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -1745,18 +1745,23 @@ class Engine: def _validate_select_field_grouping_postgres(self): """DX: In PostgreSQL, selected fields used with group by need to either be aggregated or be grouped, the Query Builder validates this rule if user is unaware""" + # string processing needed since this may break in certain queries e.g."tabDocType"."module" + clean_groups = { + str(g).replace('"', "").replace("`", "").split(".")[-1] for g in self._grouped_queries + } for field in self.fields: if isinstance(field, AggregateFunction): continue alias = getattr(field, "alias", None) - field_val = alias if alias is not None else field - field_val = str(field_val).replace('"', "") - if field_val not in self._grouped_queries: + field_alias = alias.replace('"', "").replace("`", "") if alias else None + field_source = str(field).replace('"', "").replace("`", "").split(".")[-1] + is_grouped = (field_source in clean_groups) or (field_alias in clean_groups) + if not is_grouped: frappe.throw( _( "PostgreSQL grouping error: The field '{0}' is selected but neither grouped nor aggregated. " "Add it to 'group_by' or aggregate it." - ).format(field_val), + ).format(field_alias or field_source), frappe.ValidationError, )