Skip to content

Commit

Permalink
fix: prevent ForeignKeyViolation error on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Mar 17, 2023
1 parent d950eb8 commit d1a3fb9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions superset/charts/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def run(self) -> Model:
self.validate()
try:
Dashboard.clear_cache_for_slice(slice_id=self._model_id)
# Even thought 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
2 changes: 1 addition & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def data(self) -> Dict[str, Any]:
metadata,
Column("id", Integer, primary_key=True),
Column("user_id", Integer, ForeignKey("ab_user.id")),
Column("table_id", Integer, ForeignKey("tables.id")),
Column("table_id", Integer, ForeignKey("tables.id", ondelete="CASCADE")),
)


Expand Down
4 changes: 4 additions & 0 deletions superset/datasets/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def __init__(self, model_id: int):
def run(self) -> Model:
self.validate()
try:
# Even thought 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
2 changes: 1 addition & 1 deletion superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
metadata,
Column("id", Integer, primary_key=True),
Column("user_id", Integer, ForeignKey("ab_user.id")),
Column("slice_id", Integer, ForeignKey("slices.id")),
Column("slice_id", Integer, ForeignKey("slices.id", ondelete="CASCADE")),
)
logger = logging.getLogger(__name__)

Expand Down

0 comments on commit d1a3fb9

Please sign in to comment.