diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index 7e582782e911..7c239fd720cd 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -8002,10 +8002,20 @@ def copy_to(self, copied_step, step_mapping, user=None): copied_step.annotations = annotations if subworkflow := self.subworkflow: - copied_subworkflow = subworkflow.copy() - copied_step.subworkflow = copied_subworkflow - for subworkflow_step, copied_subworkflow_step in zip(subworkflow.steps, copied_subworkflow.steps): - subworkflow_step_mapping[subworkflow_step.id] = copied_subworkflow_step + stored_subworkflow = subworkflow.stored_workflow + if stored_subworkflow and stored_subworkflow.user == user: + # This should be fine and reduces the number of stored subworkflows + copied_step.subworkflow = subworkflow + else: + # Can this even happen, building a workflow with a subworkflow you don't own ? + copied_subworkflow = subworkflow.copy() + stored_workflow = StoredWorkflow( + user, name=copied_subworkflow.name, workflow=copied_subworkflow, hidden=True + ) + copied_subworkflow.stored_workflow = stored_workflow + copied_step.subworkflow = copied_subworkflow + for subworkflow_step, copied_subworkflow_step in zip(subworkflow.steps, copied_subworkflow.steps): + subworkflow_step_mapping[subworkflow_step.id] = copied_subworkflow_step for old_conn, new_conn in zip(self.input_connections, copied_step.input_connections): new_conn.input_step_input = copied_step.get_or_add_input(old_conn.input_name)