diff --git a/python/lsst/daf/butler/_butler_collections.py b/python/lsst/daf/butler/_butler_collections.py index 71045f2937..8a3d2e2d63 100644 --- a/python/lsst/daf/butler/_butler_collections.py +++ b/python/lsst/daf/butler/_butler_collections.py @@ -384,7 +384,7 @@ def x_remove(self, name: str) -> None: ------ lsst.daf.butler.registry.MissingCollectionError Raised if no collection with the given name exists. - sqlalchemy.exc.IntegrityError + lsst.daf.butler.registry.OrphanedRecordError Raised if the database rows associated with the collection are still referenced by some other table, such as a dataset in a datastore (for `~CollectionType.RUN` collections only) or a diff --git a/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py b/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py index baa1c92176..8a4b7c70e8 100644 --- a/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py +++ b/python/lsst/daf/butler/direct_butler/_direct_butler_collections.py @@ -31,10 +31,12 @@ from collections.abc import Iterable, Sequence, Set +import sqlalchemy from lsst.utils.iteration import ensure_iterable from .._butler_collections import ButlerCollections, CollectionInfo from .._collection_type import CollectionType +from ..registry._exceptions import OrphanedRecordError from ..registry.interfaces import ChainedCollectionRecord from ..registry.sql_registry import SqlRegistry @@ -151,4 +153,7 @@ def register(self, name: str, type: CollectionType = CollectionType.RUN, doc: st return self._registry.registerCollection(name, type, doc) def x_remove(self, name: str) -> None: - self._registry.removeCollection(name) + try: + self._registry.removeCollection(name) + except sqlalchemy.exc.IntegrityError as e: + raise OrphanedRecordError(f"Datasets in run {name} are still referenced elsewhere.") from e