{"record":{"id":"09f49165b35395f9","repo":"mastra-ai/mastra","slug":"item-id-does-not-belong-to-dataset-datasetid","errorCode":null,"errorMessage":"Item ${id} does not belong to dataset ${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":347,"sourceCode":"\n    // T3.11\n    await this.createDatasetVersion(args.datasetId, newVersion);\n\n    return toDatasetItem(newRow);\n  }\n\n  protected async _doDeleteItem({ id, datasetId }: DeleteDatasetItemInput): Promise<void> {\n    const rows = this.db.datasetItems.get(id);\n    if (!rows || rows.length === 0) {\n      return; // no-op if item doesn't exist\n    }\n\n    const currentRow = rows.find(r => r.validTo === null && !r.isDeleted);\n    if (!currentRow) {\n      return; // already deleted\n    }\n    if (currentRow.datasetId !== datasetId) {\n      throw new Error(`Item ${id} does not belong to dataset ${datasetId}`);\n    }\n\n    const dataset = this.db.datasets.get(datasetId);\n    if (!dataset) {\n      throw new Error(`Dataset not found: ${datasetId}`);\n    }\n\n    // Bump version (T3.26)\n    const newVersion = dataset.version + 1;\n    this.db.datasets.set(datasetId, { ...dataset, version: newVersion });\n\n    // T3.9 — close old row\n    currentRow.validTo = newVersion;\n\n    // T3.9 — insert tombstone.\n    // Tenancy is read from the prior current row rather than re-fetched from\n    // the parent dataset (the pattern used by every DB adapter). This is\n    // deliberate and safe: tenancy is immutable post-create on both datasets","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/datasets/inmemory.ts#L329-L365","documentation":"Thrown by the in-memory dataset store when deleting an item whose current (live) SCD-2 row belongs to a different dataset than the one passed in. It means the item id exists but is not a member of the target datasetId, so the delete is refused to prevent cross-dataset mutation.","triggerScenarios":"Calling storage.datasets.deleteItem({ datasetId, id }) with a valid item id that was created in a different dataset; passing a swapped datasetId/id pair; reusing a stale item reference after the item was moved or re-created in another dataset.","commonSituations":"Copy-paste of ids across datasets in tests or scripts; iterating a mixed list of item ids against one datasetId; client cache holding items from a previous dataset version.","solutions":["Verify the item's datasetId (getItem) and pass the dataset the item actually belongs to.","Check you haven't swapped datasetId and id in the call arguments.","If the item was re-created elsewhere, delete using the id returned by the create/batchInsert call in that dataset.","If the item is already deleted, note deleteItem silently no-ops instead of throwing; only ownership mismatch throws."],"exampleFix":"// before\nawait storage.datasets.deleteItem({ datasetId: 'ds-b', id: itemId });\n// after\nconst item = await storage.datasets.getItem({ datasetId: 'ds-a', id: itemId });\nawait storage.datasets.deleteItem({ datasetId: item.datasetId, id: itemId });","handlingStrategy":"validation","validationCode":"const item = await storage.datasets.getItem({ datasetId, id });\nif (!item) throw new Error(`Item ${id} not found in dataset ${datasetId}`);","typeGuard":"function belongsToDataset(item: { datasetId: string } | null, datasetId: string): item is { datasetId: string } {\n  return item !== null && item.datasetId === datasetId;\n}","tryCatchPattern":"try {\n  await storage.datasets.deleteItem({ datasetId, id });\n} catch (e) {\n  if (String(e).includes('does not belong to dataset')) {\n    // wrong dataset — resolve the owning dataset and retry\n  } else throw e;\n}","preventionTips":["Always carry datasetId together with item id in the same record.","Resolve the owning dataset via getItem before cross-dataset operations.","Avoid hardcoding dataset ids in scripts."],"tags":["storage","datasets","inmemory","ownership-mismatch"],"backgroundTag":"resource-ownership-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}