fix: prevent ForeignKeyViolation error on delete (#23414)

This commit is contained in:
Beto Dealmeida
2023-03-21 15:48:55 -07:00
committed by GitHub
parent d01cf4300c
commit 45f045def2
2 changed files with 12 additions and 2 deletions

View File

@@ -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 @@ class DeleteChartCommand(BaseCommand):
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)

View File

@@ -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 @@ class DeleteDatasetCommand(BaseCommand):
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: