@@ -4743,26 +4743,32 @@ def _touch_collection_update_time_cte(self, session, supports_skip_locked):
47434743 literal(0).label("depth_level"),
47444744 ]
47454745
4746- # Create a single base query that covers both HDA and LDDA cases using OR conditions
4747- base_query = (
4746+ # Create separate base queries for HDA and LDDA cases and union them
4747+ # We need to wrap the union in a subquery to use it as the anchor for the recursive CTE
4748+ union_query = (
47484749 select(*base_columns)
47494750 .select_from(
4750- DatasetCollectionElement.__table__.outerjoin (
4751+ DatasetCollectionElement.__table__.join (
47514752 HistoryDatasetAssociation.__table__, DatasetCollectionElement.hda_id == HistoryDatasetAssociation.id
4752- ).outerjoin(
4753- LibraryDatasetDatasetAssociation.__table__,
4754- DatasetCollectionElement.ldda_id == LibraryDatasetDatasetAssociation.id,
47554753 )
47564754 )
4757- .where(
4758- or_(
4759- HistoryDatasetAssociation.dataset_id == self.id,
4760- LibraryDatasetDatasetAssociation.dataset_id == self.id,
4755+ .where(HistoryDatasetAssociation.dataset_id == self.id)
4756+ .union(
4757+ select(*base_columns)
4758+ .select_from(
4759+ DatasetCollectionElement.__table__.join(
4760+ LibraryDatasetDatasetAssociation.__table__,
4761+ DatasetCollectionElement.ldda_id == LibraryDatasetDatasetAssociation.id,
4762+ )
47614763 )
4764+ .where(LibraryDatasetDatasetAssociation.dataset_id == self.id)
47624765 )
4763- )
4766+ ).subquery()
4767+
4768+ # Select from the union subquery to create a proper base query for the CTE
4769+ base_query = select(union_query.c.collection_id, union_query.c.depth_level)
47644770
4765- # Create the recursive CTE from the single base query
4771+ # Create the recursive CTE from the base query
47664772 collection_hierarchy_cte = base_query.cte(name="collection_hierarchy", recursive=True)
47674773
47684774 # Create aliases for the recursive part
0 commit comments