You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/03/17 20:57:16 UTC

[superset] 01/01: fix: prevent ForeignKeyViolation error on delete

This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch sc62626
in repository https://gitbox.apache.org/repos/asf/superset.git

commit d1a3fb953b12c3dc41a43ad5679e9f9c781f8417
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Fri Mar 17 13:56:58 2023 -0700

    fix: prevent ForeignKeyViolation error on delete
---
 superset/charts/commands/delete.py   | 4 ++++
 superset/connectors/sqla/models.py   | 2 +-
 superset/datasets/commands/delete.py | 4 ++++
 superset/models/slice.py             | 2 +-
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/superset/charts/commands/delete.py b/superset/charts/commands/delete.py
index cb6644c711..1dae6a94b5 100644
--- a/superset/charts/commands/delete.py
+++ b/superset/charts/commands/delete.py
@@ -47,6 +47,10 @@ class DeleteChartCommand(BaseCommand):
         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)
diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py
index b76e423caf..36aa5e4fe0 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -520,7 +520,7 @@ sqlatable_user = Table(
     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")),
 )
 
 
diff --git a/superset/datasets/commands/delete.py b/superset/datasets/commands/delete.py
index 6f91567958..d457ad4e34 100644
--- a/superset/datasets/commands/delete.py
+++ b/superset/datasets/commands/delete.py
@@ -44,6 +44,10 @@ class DeleteDatasetCommand(BaseCommand):
     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:
diff --git a/superset/models/slice.py b/superset/models/slice.py
index 9ab4039a93..12e9ac404e 100644
--- a/superset/models/slice.py
+++ b/superset/models/slice.py
@@ -59,7 +59,7 @@ slice_user = Table(
     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__)