diff --git a/lib/galaxy/managers/users.py b/lib/galaxy/managers/users.py index 8d08d767f355..d9912dfd6651 100644 --- a/lib/galaxy/managers/users.py +++ b/lib/galaxy/managers/users.py @@ -414,19 +414,20 @@ def tags_used(self, user, tag_models=None): # get all the taggable model TagAssociations if not tag_models: tag_models = [v.tag_assoc_class for v in self.app.tag_handler.item_tag_assoc_info.values()] - # create a union of subqueries for each for this user - getting only the tname and user_value - all_tags_query = None - for tag_model in tag_models: - subq = self.session().query(tag_model.user_tname, tag_model.user_value).filter(tag_model.user == user) - all_tags_query = subq if all_tags_query is None else all_tags_query.union(subq) - # if nothing init'd the query, bail - if all_tags_query is None: + if not tag_models: return [] + # create a union of select statements for each tag model for this user - getting only the tname and user_value + all_tags_stmt = None + for tag_model in tag_models: + stmt = select(tag_model.user_tname, tag_model.user_value).where(tag_model.user == user) + all_tags_stmt = stmt if all_tags_stmt is None else all_tags_stmt.union(stmt) + # boil the tag tuples down into a sorted list of DISTINCT name:val strings - tags = all_tags_query.distinct().all() - tags = [(f"{name}:{val}" if val else name) for name, val in tags] + all_tags_stmt = all_tags_stmt.distinct() + tag_tuples = self.session().exectute(all_tags_stmt) + tags = [(f"{name}:{val}" if val else name) for name, val in tag_tuples] # consider named tags while sorting return sorted(tags, key=lambda str: re.sub("^name:", "#", str))