From f982439eb9a86561cdb0b9d449ab83a12c56e0a0 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 9 Jan 2023 16:43:44 +0530 Subject: [PATCH] fix: pass fields explicitly - to prevent addition of default `name` field - also, add fields only if it is a select query --- frappe/database/query.py | 18 +++++++++--------- frappe/query_builder/functions.py | 6 +++--- frappe/utils/goal.py | 11 +++++++---- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/frappe/database/query.py b/frappe/database/query.py index ac23097226..b9dfa33217 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -236,16 +236,16 @@ class Engine: self.query = frappe.qb.from_(self.table).delete() else: self.query = frappe.qb.from_(self.table) + # add fields + self.fields = self.parse_fields(fields) + if not self.fields: + self.fields = [getattr(self.table, pluck or "name")] - self.fields = self.parse_fields(fields) - if not self.fields: - self.fields = [getattr(self.table, pluck or "name")] - - for field in self.fields: - if isinstance(field, DynamicTableField): - self.query = field.apply_select(self.query) - else: - self.query = self.query.select(field) + for field in self.fields: + if isinstance(field, DynamicTableField): + self.query = field.apply_select(self.query) + else: + self.query = self.query.select(field) self.apply_filters(filters) self.apply_implicit_joins() diff --git a/frappe/query_builder/functions.py b/frappe/query_builder/functions.py index e1ab182553..512df8835c 100644 --- a/frappe/query_builder/functions.py +++ b/frappe/query_builder/functions.py @@ -119,9 +119,9 @@ class Cast_(Function): def _aggregate(function, dt, fieldname, filters, **kwargs): return ( - frappe.qb.get_query(dt, filters=filters) - .select(function(PseudoColumn(fieldname))) - .run(**kwargs)[0][0] + frappe.qb.get_query(dt, filters=filters, fields=[function(PseudoColumn(fieldname))]).run( + **kwargs + )[0][0] or 0 ) diff --git a/frappe/utils/goal.py b/frappe/utils/goal.py index 0dcadc5ec6..f60aec4d2b 100644 --- a/frappe/utils/goal.py +++ b/frappe/utils/goal.py @@ -24,10 +24,13 @@ def get_monthly_results( date_format = "%m-%Y" if frappe.db.db_type != "postgres" else "MM-YYYY" return dict( - frappe.qb.get_query(table=goal_doctype, filters=filters) - .select( - DateFormat(Table[date_col], date_format).as_("month_year"), - Function(aggregation, goal_field), + frappe.qb.get_query( + table=goal_doctype, + fields=[ + DateFormat(Table[date_col], date_format).as_("month_year"), + Function(aggregation, goal_field), + ], + filters=filters, ) .groupby("month_year") .run()