{"record":{"id":"45a86678a05eb162","repo":"Budibase/budibase","slug":"project-import-could-not-remap-datasource-for-quer","errorCode":null,"errorMessage":"Project import could not remap datasource for query '${doc._id}'.","messagePattern":"Project import could not remap datasource for query '(.+?)'\\.","errorType":"http","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/sdk/workspace/projects/backups/imports.ts","lineNumber":614,"sourceCode":"\nconst generateImportedId = (\n  resourceType: ResourceType,\n  doc: AnyDocument,\n  idMap: Map<string, string>\n) => {\n  switch (resourceType) {\n    case ResourceType.AGENT:\n      return docIds.generateAgentID()\n    case ResourceType.DATASOURCE:\n      return generateDatasourceID({\n        plus: !!doc._id?.startsWith(prefixed(DocumentType.DATASOURCE_PLUS)),\n      })\n    case ResourceType.TABLE:\n      return generateTableID()\n    case ResourceType.QUERY: {\n      const datasourceId = doc.datasourceId && idMap.get(doc.datasourceId)\n      if (!datasourceId) {\n        throw new HTTPError(\n          `Project import could not remap datasource for query '${doc._id}'.`,\n          400\n        )\n      }\n      return generateQueryID(datasourceId)\n    }\n    case ResourceType.AUTOMATION:\n      return generateAutomationID()\n    case ResourceType.ROW_ACTION: {\n      const tableId = idMap.get(extractTableIdFromRowActionsID(doc._id!))\n      if (!tableId) {\n        throw new HTTPError(\n          `Project import could not remap table for row actions '${doc._id}'.`,\n          400\n        )\n      }\n      return generateRowActionsID(tableId)\n    }","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/projects/backups/imports.ts#L596-L632","documentation":"During a project (workspace backup) import, every QUERY document must have its datasourceId remapped to the newly generated datasource ID via the idMap. This error is thrown when a query references a datasourceId that is missing from the import or has no mapping in idMap, meaning the parent datasource was not imported or remapped before the query. The import aborts with HTTP 400 because a query without a valid datasource cannot function.","triggerScenarios":"Importing a project export where a query doc's datasourceId is undefined or idMap.get(doc.datasourceId) returns undefined — e.g. the export contains a query but not its datasource, the datasource doc failed to import earlier, or the manifest resourcesByType omits the datasource type.","commonSituations":"Hand-edited or partially truncated export files; exports produced by older/broken export tooling that skipped datasource docs; third-party tooling generating project packages that reference datasources not included in the package.","solutions":["Ensure the export package includes every datasource document referenced by queries (check manifest.resourcesByType counts).","Re-export the project from a healthy workspace rather than hand-assembling the package.","Check the import logs for earlier failures on datasource documents that prevented their ID mapping.","If intentionally dropping datasources, also drop dependent queries from the package."],"exampleFix":"// before (package json missing datasource doc for query)\n{ \"queries\": [{ \"_id\": \"query_def1\", \"datasourceId\": \"datasource_missing\" }] }\n// after (include the datasource so idMap can remap)\n{ \"datasources\": [{ \"_id\": \"datasource_missing\" }], \"queries\": [{ \"_id\": \"query_def1\", \"datasourceId\": \"datasource_missing\" }] }","handlingStrategy":"validation","validationCode":"const dsIds = new Set(docs.filter(d => d.type === ResourceType.DATASOURCE).map(d => d._id))\nfor (const q of docs.filter(d => d.type === ResourceType.QUERY)) {\n  if (!q.datasourceId || !dsIds.has(q.datasourceId)) throw new Error(`Query ${q._id} references missing datasource`)\n}","typeGuard":"const hasDatasource = (q: Query): q is Query & { datasourceId: string } =>\n  typeof q.datasourceId === \"string\" && q.datasourceId.length > 0","tryCatchPattern":"try {\n  await importProjectPackage(file)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 400 && err.message.includes(\"could not remap datasource\")) {\n    // surface which query doc failed: parse id from err.message\n  }\n  throw err\n}","preventionTips":["Always export via Budibase's own export tooling, never hand-assemble packages","Verify manifest.resourcesByType datasource counts match the datasources referenced by queries","Re-download exports if the archive size looks wrong"],"tags":["import","id-mapping","datasource","project-package"],"backgroundTag":"import-id-remap-missing-parent","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}