From 71f7ae774910fb0a711f1f4449e79710f7b14064 Mon Sep 17 00:00:00 2001 From: John Davis Date: Thu, 26 Oct 2023 15:17:58 -0400 Subject: [PATCH] Explicitly select all columns that appear in order_by Ref: https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#using-distinct-with-additional-columns-but-only-select-the-entity Note: this query-building code needs to be converted to SA 2.0; but same requirement applies. --- lib/galaxy/model/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index b261a9b8337d..6b605fee2f17 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -6170,6 +6170,12 @@ def attribute_columns(column_collection, attributes, nesting_level=None): q = q.add_entity(entity) if entity == DatasetCollectionElement: q = q.filter(entity.id == dce.c.id) + + # Since we will apply DISTINCT, ensure all columns from the ORDER BY clause are explicitly selected + for col in order_by_columns: + if col not in q.statement._raw_columns: # do not select a column more than once. + q = q.add_column(col) + return q.distinct().order_by(*order_by_columns) @property