Skip to content

Commit

Permalink
#223: Fix bug where single related distinct field was given
Browse files Browse the repository at this point in the history
- This fixes requests such as: `/investigations?distinct=["investigationtype.createTime"]`
  • Loading branch information
MRichards99 committed May 14, 2021
1 parent 3916a56 commit 0dfa7a3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions datagateway_api/common/database/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ class DatabaseFilterUtilities:
"""

def __init__(self):
"""
The `distinct_join_flag` tracks if JOINs need to be added to the query - on a
distinct filter, if there's no unrelated fields (i.e. no fields with a
`related_depth` of 1), adding JOINs to the query (using `_add_query_join()`)
will result in a `sqlalchemy.exc.InvalidRequestError`
"""
self.field = None
self.related_field = None
self.related_related_field = None
self.distinct_join_flag = False

def _extract_filter_fields(self, field):
"""
Expand All @@ -56,6 +63,7 @@ def _extract_filter_fields(self, field):

if related_depth == 1:
self.field = fields[0]
self.distinct_join_flag = True
elif related_depth == 2:
self.field = fields[0]
self.related_field = fields[1]
Expand Down Expand Up @@ -171,9 +179,10 @@ def apply_filter(self, query):
# SELECT multiple and effectively turn the query into a `SELECT *`
query.base_query = query.session.query(*distinct_fields).distinct()

for field_name in self.fields:
self._extract_filter_fields(field_name)
self._add_query_join(query)
if self.distinct_join_flag:
for field_name in self.fields:
self._extract_filter_fields(field_name)
self._add_query_join(query)
except AttributeError:
raise FilterError("Bad field requested")

Expand Down

0 comments on commit 0dfa7a3

Please sign in to comment.