Skip to content

Commit

Permalink
fix: prevent ForeignKeyViolation error on delete (#23414)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored Mar 21, 2023
1 parent d01cf43 commit 45f045d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion superset/charts/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion superset/datasets/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 45f045d

Please sign in to comment.