浏览代码

Merge pull request #8672 from scmmishra/group-by-fix

fix: Do not append table name for field with standard sql method
version-14
Shivam Mishra 5 年前
committed by GitHub
父节点
当前提交
7eb322f2be
找不到此签名对应的密钥 GPG 密钥 ID: 4AEE18F83AFDEB23
共有 3 个文件被更改,包括 19 次插入4 次删除
  1. +1
    -1
      frappe/desk/listview.py
  2. +7
    -3
      frappe/model/db_query.py
  3. +11
    -0
      frappe/tests/test_db_query.py

+ 1
- 1
frappe/desk/listview.py 查看文件

@@ -45,7 +45,7 @@ def get_group_by_count(doctype, current_filters, field):
order by
count desc
limit 50""".format(subquery_condition = subquery_condition), as_dict=True)
else :
else:
return frappe.db.get_list(doctype,
filters=current_filters,
group_by=field,


+ 7
- 3
frappe/model/db_query.py 查看文件

@@ -284,10 +284,14 @@ class DatabaseQuery(object):
def set_field_tables(self):
'''If there are more than one table, the fieldname must not be ambiguous.
If the fieldname is not explicitly mentioned, set the default table'''
def _in_standard_sql_methods(field):
methods = ('count(', 'avg(', 'sum(')
return field.lower().startswith(methods)

if len(self.tables) > 1:
for i, f in enumerate(self.fields):
if '.' not in f:
self.fields[i] = '{0}.{1}'.format(self.tables[0], f)
for idx, field in enumerate(self.fields):
if '.' not in field and not _in_standard_sql_methods(field):
self.fields[idx] = '{0}.{1}'.format(self.tables[0], field)

def set_optional_columns(self):
"""Removes optional columns like `_user_tags`, `_comments` etc. if not in table"""


+ 11
- 0
frappe/tests/test_db_query.py 查看文件

@@ -6,6 +6,7 @@ import frappe, unittest

from frappe.model.db_query import DatabaseQuery
from frappe.desk.reportview import get_filters_cond

from frappe.permissions import add_user_permission, clear_user_permissions_for_doctype

test_dependencies = ['User', 'Blog Post']
@@ -332,6 +333,16 @@ class TestReportview(unittest.TestCase):
self.assertTrue({'name': 'Prepared Report'} in res)
self.assertFalse({'name': 'Property Setter'} in res)

def test_set_field_tables(self):
# Tests _in_standard_sql_methods method in test_set_field_tables
# The following query will break if the above method is broken
data = frappe.db.get_list("Web Form",
filters=[['Web Form Field', 'reqd', '=', 1]],
group_by='amount_field',
fields=['count(*) as count', '`amount_field` as name'],
order_by='count desc',
limit=50,
)

def create_event(subject="_Test Event", starts_on=None):
""" create a test event """


正在加载...
取消
保存