Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow chart import to update the dataset an existing chart points to #24821

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion superset/charts/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def import_chart(
# migrate old viz types to new ones
config = migrate_chart(config)

chart = Slice.import_from_dict(session, config, recursive=False)
chart = Slice.import_from_dict(
session, config, recursive=False, allow_reparenting=True
)
if chart.id is None:
session.flush()

Expand Down
14 changes: 9 additions & 5 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ImportExportMixin:
__mapper__: Mapper

@classmethod
def _unique_constrains(cls) -> list[set[str]]:
def _unique_constraints(cls) -> list[set[str]]:
"""Get all (single column and multi column) unique constraints"""
unique = [
{c.name for c in u.columns}
Expand Down Expand Up @@ -244,6 +244,7 @@ def import_from_dict(
parent: Optional[Any] = None,
recursive: bool = True,
sync: Optional[list[str]] = None,
allow_reparenting: bool = False,
) -> Any:
"""Import obj from a dictionary"""
if sync is None:
Expand All @@ -256,7 +257,7 @@ def import_from_dict(
| {"uuid"}
)
new_children = {c: dict_rep[c] for c in cls.export_children if c in dict_rep}
unique_constrains = cls._unique_constrains()
unique_constraints = cls._unique_constraints()

filters = [] # Using these filters to check if obj already exists

Expand All @@ -275,8 +276,11 @@ def import_from_dict(
for k, v in parent_refs.items():
dict_rep[k] = getattr(parent, v)

# Add filter for parent obj
filters.extend([getattr(cls, k) == dict_rep.get(k) for k in parent_refs.keys()])
if not allow_reparenting:
# Add filter for parent obj
filters.extend(
[getattr(cls, k) == dict_rep.get(k) for k in parent_refs.keys()]
)

# Add filter for unique constraints
ucs = [
Expand All @@ -287,7 +291,7 @@ def import_from_dict(
if dict_rep.get(k) is not None
]
)
for cs in unique_constrains
for cs in unique_constraints
]
filters.append(or_(*ucs))

Expand Down
Loading