{"record":{"id":"f0cbfb39a3e16dd1","repo":"mastra-ai/mastra","slug":"cannot-favorite-entitytype-with-id-entityid","errorCode":null,"errorMessage":"Cannot favorite: ${entityType} with id ${entityId} does not exist","messagePattern":"Cannot favorite: (.+?) with id (.+?) does not exist","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/favorites/inmemory.ts","lineNumber":147,"sourceCode":"    if (entity && entity.favoriteCount) {\n      entity.favoriteCount = 0;\n    }\n    return removed;\n  }\n\n  /**\n   * Look up the parent entity record for counter maintenance. Throws if the\n   * entity does not exist — callers should validate existence (and access)\n   * before invoking favorite/unfavorite.\n   */\n  private requireEntity(\n    entityType: StorageFavoriteEntityType,\n    entityId: string,\n  ): { favoriteCount?: number; updatedAt: Date } {\n    const map = entityType === 'agent' ? this.db.agents : this.db.skills;\n    const entity = map.get(entityId);\n    if (!entity) {\n      throw new Error(`Cannot favorite: ${entityType} with id ${entityId} does not exist`);\n    }\n    return entity;\n  }\n}\n","sourceCodeStart":129,"sourceCodeEnd":152,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/favorites/inmemory.ts#L129-L152","documentation":"This error is thrown by the in-memory favorites storage's `requireEntity` helper when an agent or skill being favorited does not exist in the store. The library enforces that favorites can only reference real entities, so `favorite`/`unfavorite` operations call `requireEntity` first and abort with this message if the lookup map returns nothing. It prevents orphaned favorite records pointing at nonexistent agents or skills.","triggerScenarios":"Calling `favorite(entityType, entityId)` (or unfavorite variants that also call `requireEntity`) with an entityId that was never added to `this.db.agents` or `this.db.skills`, or with an id whose entry was deleted, or with an `entityType` that routes to the wrong map (only 'agent' and 'skill' are supported).","commonSituations":"Passing a hardcoded or stale test id; favoriting an entity in a fresh store that was never seeded via create/upsert; a race where the agent/skill is deleted between listing and favoriting; typos in id strings; using an id from a different store instance (e.g. per-request in-memory db instances).","solutions":["Create/upsert the agent or skill with the given id in the store before favoriting it.","Verify the entityId against a list of existing entities (e.g. `listAgents()`/`listSkills()`) before calling favorite.","Check that you are using the same storage instance where the entity was created — a new in-memory store starts empty.","Confirm `entityType` is exactly 'agent' or 'skill' and matches how the entity was stored."],"exampleFix":"// before\nawait storage.favorite('agent', 'agent-123'); // throws if not created\n// after\nawait storage.createAgent({ id: 'agent-123', /* ... */ });\nawait storage.favorite('agent', 'agent-123');","handlingStrategy":"validation","validationCode":"const exists = entityType === 'agent'\n  ? await storage.listAgents().then(a => a.some(x => x.id === entityId))\n  : await storage.listSkills().then(s => s.some(x => x.id === entityId));\nif (!exists) throw new Error(`${entityType} ${entityId} not created yet`);\nawait storage.favorite(entityType, entityId);","typeGuard":"function isKnownEntity(entity: { id: string } | undefined, id: string): entity is { id: string } {\n  return entity !== undefined && entity.id === id;\n}","tryCatchPattern":"try {\n  await storage.favorite(entityType, entityId);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('does not exist')) {\n    // create the entity first or skip favoriting\n    return null;\n  }\n  throw e;\n}","preventionTips":["Always create/upsert the entity before favoriting in the same storage instance.","Share one storage instance across your app (DI) rather than constructing ad-hoc in-memory stores.","Validate entityType is 'agent' or 'skill' with a union type at compile time.","In tests, seed fixtures before exercising favorite logic."],"tags":["storage","in-memory","entity-not-found","favorites"],"backgroundTag":"entity-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}