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