diff --git a/superset/charts/commands/delete.py b/superset/charts/commands/delete.py index cb6644c711c45..4c636f0433a73 100644 --- a/superset/charts/commands/delete.py +++ b/superset/charts/commands/delete.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. import logging -from typing import Optional +from typing import cast, Optional from flask_appbuilder.models.sqla import Model from flask_babel import lazy_gettext as _ @@ -45,8 +45,13 @@ def __init__(self, model_id: int): def run(self) -> Model: self.validate() + self._model = cast(Slice, self._model) try: Dashboard.clear_cache_for_slice(slice_id=self._model_id) + # Even though SQLAlchemy should in theory delete rows from the association + # table, sporadically Superset will error because the rows are not deleted. + # Let's do it manually here to prevent the error. + self._model.owners = [] chart = ChartDAO.delete(self._model) except DAODeleteFailedError as ex: logger.exception(ex.exception) diff --git a/superset/datasets/commands/delete.py b/superset/datasets/commands/delete.py index 6f91567958135..1487f1028b3be 100644 --- a/superset/datasets/commands/delete.py +++ b/superset/datasets/commands/delete.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. import logging -from typing import Optional +from typing import cast, Optional from flask_appbuilder.models.sqla import Model from sqlalchemy.exc import SQLAlchemyError @@ -43,7 +43,12 @@ def __init__(self, model_id: int): def run(self) -> Model: self.validate() + self._model = cast(SqlaTable, self._model) try: + # Even though SQLAlchemy should in theory delete rows from the association + # table, sporadically Superset will error because the rows are not deleted. + # Let's do it manually here to prevent the error. + self._model.owners = [] dataset = DatasetDAO.delete(self._model, commit=False) db.session.commit() except (SQLAlchemyError, DAODeleteFailedError) as ex: