diff --git a/frappe/database/query.py b/frappe/database/query.py index 837915f34c..d5d2dd1aec 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -345,8 +345,6 @@ class Engine: self.query._fields_list = getattr(self, "fields", []) self.query.immutable = True - if self.is_postgres and self.is_aggregate_query: - self._validate_select_field_grouping_postgres() # DX: validate query return self.query def validate_doctype(self): @@ -1743,29 +1741,6 @@ class Engine: return True - 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_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_alias or field_source), - frappe.ValidationError, - ) - class DynamicTableField: def __init__( diff --git a/frappe/tests/test_query.py b/frappe/tests/test_query.py index 5a73d7a310..2962918316 100644 --- a/frappe/tests/test_query.py +++ b/frappe/tests/test_query.py @@ -2361,21 +2361,6 @@ class TestQuery(IntegrationTestCase): self.assertQueryEqual(query, 'SELECT COUNT(*) "result" FROM "tabUser" ORDER BY MAX("creation") DESC') - @run_only_if(db_type_is.POSTGRES) - def test_query_validation_postgres(self): - """PostgreSQL specific test that tests if query that is built is valid in PostgreSQL, as part of a better DX""" - with self.assertRaises(frappe.ValidationError) as pgerr: - frappe.qb.get_query( - "User", - fields=["user_type as type", "enabled as status", {"COUNT": "*"}], - group_by="type", - order_by="type", - ).run() - self.assertEqual( - str(pgerr.exception), - "PostgreSQL grouping error: The field 'status' is selected but neither grouped nor aggregated. Add it to 'group_by' or aggregate it.", - ) - # This function is used as a permission query condition hook def test_permission_hook_condition(user):