{"record":{"id":"0347c7e2b2f6896a","repo":"mastra-ai/mastra","slug":"item-args-id-does-not-belong-to-dataset-args","errorCode":null,"errorMessage":"Item ${args.id} does not belong to dataset ${args.datasetId}","messagePattern":"Item (.+?) does not belong to dataset (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/datasets/inmemory.ts","lineNumber":287,"sourceCode":"\n    // T3.11 — every mutation inserts exactly one dataset_version row\n    await this.createDatasetVersion(args.datasetId, newVersion);\n\n    return toDatasetItem(row);\n  }\n\n  protected async _doUpdateItem(args: UpdateDatasetItemInput): Promise<DatasetItem> {\n    const rows = this.db.datasetItems.get(args.id);\n    if (!rows || rows.length === 0) {\n      throw new Error(`Item not found: ${args.id}`);\n    }\n\n    const currentRow = rows.find(r => r.validTo === null && !r.isDeleted);\n    if (!currentRow) {\n      throw new Error(`Item not found: ${args.id}`);\n    }\n    if (currentRow.datasetId !== args.datasetId) {\n      throw new Error(`Item ${args.id} does not belong to dataset ${args.datasetId}`);\n    }\n\n    const dataset = this.db.datasets.get(args.datasetId);\n    if (!dataset) {\n      throw new Error(`Dataset not found: ${args.datasetId}`);\n    }\n\n    // Bump version (T3.26)\n    const newVersion = dataset.version + 1;\n    this.db.datasets.set(args.datasetId, { ...dataset, version: newVersion });\n\n    // T3.8 — close old row\n    currentRow.validTo = newVersion;\n\n    // T3.8 — insert new row with same id\n    const now = new Date();\n    const newRow: DatasetItemRow = {\n      id: args.id,","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/datasets/inmemory.ts#L269-L305","documentation":"InMemory _doUpdateItem() verifies that the live item row's datasetId equals args.datasetId and throws this plain Error when the item belongs to a different dataset. This guards against moving or editing an item under the wrong parent dataset when the caller supplies mismatched ids.","triggerScenarios":"updateItem({id, datasetId}) where the item id exists (live row) but was created in a dataset other than the datasetId passed — e.g., ids swapped in config, an item id reused across datasets, or a copy/paste mixing dataset ids.","commonSituations":"Bulk update scripts iterating items while hardcoding one datasetId, test fixtures where item ids collide across datasets, or callers passing the wrong relationship (item of dataset A updated with dataset B's id).","solutions":["Derive datasetId from the item itself (getItemById({id}).datasetId) instead of hardcoding it.","Fix the mapping in your update script so each item is paired with its actual parent dataset.","If the item truly must belong to another dataset, delete it there and re-insert it into the target dataset.","Log both ids in the message when debugging — the error names both the item and the dataset."],"exampleFix":"// before\nawait storage.datasets.updateItem({ id: itemId, datasetId: 'ds-a', groundTruth });\n// after\nconst item = await storage.datasets.getItemById({ id: itemId });\nawait storage.datasets.updateItem({ id: itemId, datasetId: item.datasetId, groundTruth });","handlingStrategy":"type-guard","validationCode":"const item = await storage.datasets.getItemById({ id: itemId });\nif (item && item.datasetId !== datasetId) throw new Error(`Item '${itemId}' belongs to '${item.datasetId}', not '${datasetId}'`);","typeGuard":"function belongsToDataset(item: { datasetId: string }, datasetId: string): boolean {\n  return item.datasetId === datasetId;\n}","tryCatchPattern":"try {\n  await storage.datasets.updateItem({ id: itemId, datasetId, input });\n} catch (e) {\n  if (e instanceof Error && /does not belong to dataset/.test(e.message)) {\n    // fix the item->dataset pairing in the caller before retrying\n  } else throw e;\n}","preventionTips":["Derive datasetId from the fetched item rather than hardcoding it in loops/config.","Never assume item ids are globally reusable across datasets.","In bulk scripts, group operations per item.datasetId."],"tags":["storage","datasets","in-memory","ownership"],"backgroundTag":"item-dataset-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}