superset: Import failed on duplicated key when importing existing chart that its dataset was changed

Every time we import a dashboard with chart that its dataset was changed, we’re Getting the error: psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint “uq_slices_uuid”

How to reproduce the bug

  1. export dashboard from source env and import it to target env
  2. On the source env:
    1. Go to dashboard
    2. Edit the chart
    3. change its dataset to another
    4. save (overwrite) the chart and the dashboard
    5. export the dashboard
  3. import the dashboard(2.5) on the target env with overwrite option.

Expected results

The import will overwrite the existing chart on the target env and update it to the new dataset (which by the way exists already on the target env)

Actual results

The import failed with the error: psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint “uq_slices_uuid”

Screenshots

Traceback (most recent call last): File "/app/superset/cli/importexport.py", line 163, in import_dashboards ImportDashboardsCommand(contents, overwrite=True).run() File "/app/superset/dashboards/commands/importers/dispatcher.py", line 68, in run raise exc File "/app/superset/dashboards/commands/importers/dispatcher.py", line 57, in run command.run() File "/app/superset/commands/importers/v1/__init__.py", line 72, in run raise self.import_error() from ex superset.dashboards.commands.exceptions.DashboardImportError: Import dashboard failed for an unknown reason 2022-12-22 10:57:29,174:ERROR:superset.cli.importexport:There was an error when importing the dashboards(s), please check the exception traceback in the log Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context self.dialect.do_execute( File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 608, in do_execute cursor.execute(statement, parameters) psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "uq_slices_uuid" DETAIL: Key (uuid)=(8eab9982-aaaa-450c-bbbb-1761d8c207f6) already exists.

Environment

  • browser type and version: chrome, version 108.0.5359.124
  • superset version: Superset 2.0.0
  • python version: Python 3.8.12
  • any feature flags active: FEATURE_FLAGS = { “DYNAMIC_PLUGINS”: True, “ENABLE_TEMPLATE_PROCESSING”: True, “VERSIONED_EXPORT”: True, “DASHBOARD_RBAC”: True, “DASHBOARD_CROSS_FILTERS”: True }

Checklist

Make sure to follow these steps before submitting your issue - thank you!

  • [ V ] I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • [ V ] I have reproduced the issue with at least the latest released version of superset.
  • [ V ] I have checked the issue tracker for the same issue and I haven’t found one similar.

Additional context

When I delete the chart from the target env and reimport it succeeded (unless there is another chart with the same change) In the code I see there is a select from tables before the insert into slices which probably makes that issue.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 16 (1 by maintainers)

Most upvoted comments

any updates on that issue ? I can confirm that it’s still happening on the main branch.

https://github.com/apache/superset/pull/24821 was merged which should resolve this issue; it’s currently tagged for the 2.1.2 and 3.0 releases, so it should be included in both of those.

The problem is a query to the database that determines the existence of the chart. It contains the condition WHERE slices.datasource_id = %(datasource_id_1)s AND slices.uuid = %(uuid_1)s Because of this, the existence of the diagram is determined incorrectly and is not overwritten. The query execute in a file https://github.com/apache/superset/blob/master/superset/models/helpers.py#L300