{"record":{"id":"dc03c75e94ded257","repo":"twentyhq/twenty","slug":"workspace-custom-application-not-found-for-workspa","errorCode":null,"errorMessage":"Workspace custom application not found for workspace ${workspaceId}","messagePattern":"Workspace custom application not found for workspace (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/twenty-server/src/database/commands/upgrade-version-command/2-22/2-22-workspace-command-1784193207000-backfill-workflow-version-core-links.command.ts","lineNumber":87,"sourceCode":"      this.logger.log(\n        `[DRY RUN] Would rebuild ${workspaceWorkflowVersions.length} core workflowVersion row(s) for workspace ${workspaceId}`,\n      );\n\n      return;\n    }\n\n    const queryRunner = dataSource.createQueryRunner();\n\n    await queryRunner.connect();\n\n    try {\n      const [workspace] = await queryRunner.query(\n        `SELECT \"databaseSchema\", \"workspaceCustomApplicationId\" FROM core.\"workspace\" WHERE id = $1`,\n        [workspaceId],\n      );\n\n      if (!isDefined(workspace?.workspaceCustomApplicationId)) {\n        throw new Error(\n          `Workspace custom application not found for workspace ${workspaceId}`,\n        );\n      }\n\n      const schema: string = workspace.databaseSchema;\n      const applicationId: string = workspace.workspaceCustomApplicationId;\n\n      await queryRunner.startTransaction();\n\n      try {\n        await queryRunner.query(\n          `DELETE FROM core.\"workflowVersion\" WHERE \"workspaceId\" = $1`,\n          [workspaceId],\n        );\n\n        for (const workflowVersion of workspaceWorkflowVersions) {\n          const coreWorkflowVersionId = uuidv4();\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/twentyhq/twenty/blob/1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6/packages/twenty-server/src/database/commands/upgrade-version-command/2-22/2-22-workspace-command-1784193207000-backfill-workflow-version-core-links.command.ts#L69-L105","documentation":"Thrown by the 2.22 command that rebuilds core workflowVersion rows and links them via coreWorkflowVersionId. The command opens a queryRunner, runs raw SQL to fetch the workspace's databaseSchema and workspaceCustomApplicationId from core.workspace. If workspaceCustomApplicationId is null or the workspace row does not exist, the command throws because it needs the application ID to INSERT core workflowVersion rows with the correct applicationId foreign key.","triggerScenarios":"The workspaceId passed to runOnWorkspace has no row in core.workspace, or the row exists but workspaceCustomApplicationId is null. This means workspace provisioning never created (or deleted) the custom application record.","commonSituations":"Workspace provisioning failed partway through (workspace row created, custom application not); the workspace was soft-deleted or is in a cleanup state; a manual DB operation nullified the column; testing against a workspace ID that doesn't exist.","solutions":["Verify the workspace row: SELECT id, \"workspaceCustomApplicationId\" FROM core.\"workspace\" WHERE id = $workspaceId","If workspaceCustomApplicationId is null, check the application table for the workspace's custom application and update the column, or re-provision the workspace","If the workspace is in a deleted/suspended state, exclude it from the upgrade iterator","Check workspace provisioning logs for errors during custom application creation"],"exampleFix":"// before: throws if workspaceCustomApplicationId is null\nif (!isDefined(workspace?.workspaceCustomApplicationId)) {\n  throw new Error(`Workspace custom application not found for workspace ${workspaceId}`);\n}\n\n// after: skip with a warning instead of crashing the upgrade\nif (!isDefined(workspace?.workspaceCustomApplicationId)) {\n  this.logger.warn(`Workspace ${workspaceId} has no custom application, skipping core link backfill`);\n  return;\n}","handlingStrategy":"validation","validationCode":"// Before running the upgrade, verify the workspace has a custom application\nconst result = await dataSource.query(\n  `SELECT id, \"workspaceCustomApplicationId\" FROM core.\"workspace\" WHERE id = $1`,\n  [workspaceId],\n);\n\nif (!result[0] || !result[0].workspaceCustomApplicationId) {\n  console.error(`Workspace ${workspaceId} has no custom application — skip or re-provision`);\n  // Do not run this command for this workspace\n}","typeGuard":"import { isDefined } from 'twenty-shared/utils';\n\ninterface WorkspaceRow {\n  databaseSchema: string;\n  workspaceCustomApplicationId: string | null;\n}\n\nconst hasCustomApplication = (\n  workspace: WorkspaceRow | undefined,\n): workspace is WorkspaceRow & { workspaceCustomApplicationId: string } =>\n  isDefined(workspace) && isDefined(workspace.workspaceCustomApplicationId);","tryCatchPattern":null,"preventionTips":["Verify workspace provisioning completed (custom application exists) before running upgrade commands","Exclude workspaces in deleted/suspended states from the upgrade iterator","Monitor provisioning for workspaces that fail to create the custom application","Run a pre-upgrade check: SELECT count(*) FROM core.\"workspace\" WHERE \"workspaceCustomApplicationId\" IS NULL"],"tags":["metadata","upgrade-command","workspace-data","data-integrity","workflow","raw-sql"],"backgroundTag":null,"analyzedSha":"1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6","analyzedAt":"2026-08-12T15:37:27.593Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}