diff --git a/src/spyglass/utils/dj_merge_tables.py b/src/spyglass/utils/dj_merge_tables.py index 159ec46ad..cc7dccd89 100644 --- a/src/spyglass/utils/dj_merge_tables.py +++ b/src/spyglass/utils/dj_merge_tables.py @@ -208,12 +208,16 @@ def _merge_repr(cls, restriction: str = True) -> dj.expression.Union: ) ] + if not parts: + print(f"No entries found for restriction: {restriction}") + return + primary_attrs = list( dict.fromkeys( # get all columns from parts iter_chain.from_iterable([p.heading.names for p in parts]) ) ) - # primary_attrs.append(cls()._reserved_sk) + query = dj.U(*primary_attrs) * parts[0].proj( # declare query ..., # include all attributes from part 0 **{ @@ -445,10 +449,19 @@ def merge_delete_parent( if dry_run: return part_parents - with cls._safe_context(): - super().delete(cls(), **kwargs) - for part_parent in part_parents: - super().delete(part_parent, **kwargs) + merge_ids = cls.merge_restrict(restriction).fetch( + RESERVED_PRIMARY_KEY, as_dict=True + ) + + # CB: Removed transaction protection here bc 'no' confirmation resp + # still resulted in deletes. If re-add, consider transaction=False + super().delete((cls & merge_ids), **kwargs) + + if cls & merge_ids: # If 'no' on del prompt from above, skip below + return # User can still abort del below, but yes/no is unlikly + + for part_parent in part_parents: + super().delete(part_parent, **kwargs) # add safemode=False? @classmethod def fetch_nwb( @@ -541,8 +554,8 @@ def merge_get_part( if not multi_source and len(sources) != 1: raise ValueError( - f"Found multiple potential parts: {sources}\n\t" - + "Try adding a restriction before invoking `get_part`.\n\t" + f"Found {len(sources)} potential parts: {sources}\n\t" + + "\n\tTry adjusting the restriction. " + "Or permitting multiple sources with `multi_source=True`." ) @@ -593,8 +606,8 @@ def merge_get_parent( if not multi_source and len(part_parents) != 1: raise ValueError( - f"Found multiple potential parents: {part_parents}\n\t" - + "Try adding a string restriction when invoking `get_parent`." + f"Found {len(part_parents)} possible parents: {part_parents}" + + "\n\tTry adjusting the restriction. " + "Or permitting multiple sources with `multi_source=True`." ) @@ -664,12 +677,6 @@ def merge_populate(source: str, key=None): # Aliased because underscore otherwise excludes from API docs. -_Merge = Merge - -# Underscore as class name avoids errors when this included in a Diagram -# Aliased because underscore otherwise excludes from API docs. - - def delete_downstream_merge( table: dj.Table, restriction: str = True, dry_run=True, **kwargs ) -> list: