{"record":{"id":"d016dcb481fcd49c","repo":"apache/dolphinscheduler","slug":"switch-workflow-definition-version-not-exist-workf-d016dc","errorCode":"SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR","errorMessage":"SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR: switch workflow definition version error because workflow definition version [{1}] of [{0}] does not exist","messagePattern":"SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR: switch workflow definition version error because workflow definition version \\[(.+?)\\] of \\[(.+?)\\] does not exist","errorType":"error_code","errorClass":"ServiceException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java","lineNumber":1602,"sourceCode":"        Project project = projectDao.queryByCode(projectCode);\n        projectService.checkHasProjectWritePermissionThrowException(loginUser, project);\n\n        WorkflowDefinition workflowDefinition = workflowDefinitionDao.queryByCode(code).orElse(null);\n        if (Objects.isNull(workflowDefinition) || projectCode != workflowDefinition.getProjectCode()) {\n            log.error(\n                    \"Switch workflow definition error because it does not exist, projectCode:{}, workflowDefinitionCode:{}.\",\n                    projectCode, code);\n            throw new ServiceException(Status.SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_ERROR,\n                    code);\n        }\n\n        WorkflowDefinitionLog workflowDefinitionLog =\n                workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(code, version);\n        if (Objects.isNull(workflowDefinitionLog)) {\n            log.error(\n                    \"Switch workflow definition error because version does not exist, projectCode:{}, workflowDefinitionCode:{}, version:{}.\",\n                    projectCode, code, version);\n            throw new ServiceException(\n                    Status.SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR,\n                    workflowDefinition.getCode(), version);\n        }\n        List<WorkflowTaskRelation> workflowTaskRelationList = workflowTaskRelationDao\n                .queryWorkflowTaskRelationsByWorkflowDefinitionCode(workflowDefinitionLog.getCode(),\n                        workflowDefinitionLog.getVersion());\n        List<TaskCodeVersionDto> taskDefinitionList = getTaskCodeVersionDtos(workflowTaskRelationList);\n        List<TaskDefinitionLog> taskDefinitionLogList =\n                taskDefinitionLogMapper.queryByTaskDefinitions(taskDefinitionList.stream()\n                        .flatMap(taskCodeVersionDto -> {\n                            TaskDefinitionLog taskDefinitionLog = new TaskDefinitionLog();\n                            taskDefinitionLog.setCode(taskCodeVersionDto.getCode());\n                            taskDefinitionLog.setVersion(taskCodeVersionDto.getVersion());\n                            return Stream.of(taskDefinitionLog);\n                        }).collect(Collectors.toList()));\n        taskDatasourcePermissionChecker.checkPermission(loginUser, taskDefinitionLogList);\n        taskSubWorkflowPermissionChecker.checkPermission(loginUser, taskDefinitionLogList);\n","sourceCodeStart":1584,"sourceCodeEnd":1620,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkflowDefinitionServiceImpl.java#L1584-L1620","documentation":"Thrown by switchWorkflowDefinitionVersion when the workflow definition exists but no WorkflowDefinitionLog row exists for the requested (code, version) pair — i.e. that version was never saved or was purged. workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion returned null, so there is nothing to switch to.","triggerScenarios":"Calling the version-switch endpoint with a version number that was never created for the workflow (e.g. version 5 when only 1-3 exist), or a version that was removed by log cleanup/DB pruning.","commonSituations":"Hard-coding version numbers in scripts; DB housekeeping jobs or manual cleanup deleted t_ds_workflow_definition_log rows; querying a workflow cloned into a new project where history did not carry over.","solutions":["List available versions first (GET .../workflow-definition/{code}/versions) and switch only to a version in that list.","If the version was purged from t_ds_workflow_definition_log, restore it from a backup or re-save the definition to create a new version.","Update automation to fetch the latest/existing version dynamically instead of hard-coding version numbers.","Check DB retention/cleanup settings if versions disappear unexpectedly."],"exampleFix":"// before\nworkflowDefinitionService.switchWorkflowDefinitionVersion(user, projectCode, code, 7);\n// after\nMap<String, Object> versions = workflowDefinitionService.queryWorkflowDefinitionVersions(user, projectCode, code, 1, 10, null);\nint existingVersion = /* pick from versions list */ 3;\nworkflowDefinitionService.switchWorkflowDefinitionVersion(user, projectCode, code, existingVersion);","handlingStrategy":"validation","validationCode":"boolean versionExists(long code, int version) {\n    return workflowDefinitionLogMapper.queryByDefinitionCodeAndVersion(code, version) != null;\n}\n// or via API: GET /projects/{projectCode}/workflow-definition/{code}/versions before switching","typeGuard":null,"tryCatchPattern":"try {\n    workflowDefinitionService.switchWorkflowDefinitionVersion(user, projectCode, code, version);\n} catch (ServiceException e) {\n    if (e.getCode() == Status.SWITCH_WORKFLOW_DEFINITION_VERSION_NOT_EXIST_WORKFLOW_DEFINITION_VERSION_ERROR) {\n        // fetch available versions and pick an existing one\n    }\n}","preventionTips":["Always list available versions via the versions API before switching.","Never hard-code version numbers in scripts; read the latest/available version dynamically.","Check DB cleanup/retention jobs if historical versions disappear from t_ds_workflow_definition_log."],"tags":["workflow-version","not-found","version-history","rest-api"],"backgroundTag":"record-not-found","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}