{"record":{"id":"52965504585c2d1b","repo":"nextauthjs/next-auth","slug":"account-not-found","errorCode":null,"errorMessage":"Account not found","messagePattern":"Account not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/adapter-mikro-orm/src/index.ts","lineNumber":159,"sourceCode":"    // @ts-expect-error\n    async linkAccount(data) {\n      const em = await getEM()\n      const user = await em.findOne(UserModel, { id: data.userId })\n      if (!user) throw new Error(\"User not found\")\n      const account = new AccountModel()\n      wrap(account).assign(data as object)\n      user.accounts.add(account)\n      await em.persistAndFlush(user)\n\n      return wrap(account).toObject()\n    },\n    // @ts-expect-error\n    async unlinkAccount(provider_providerAccountId) {\n      const em = await getEM()\n      const account = await em.findOne(AccountModel, {\n        ...provider_providerAccountId,\n      })\n      if (!account) throw new Error(\"Account not found\")\n      await em.removeAndFlush(account)\n\n      return wrap(account).toObject()\n    },\n    async getSessionAndUser(sessionToken) {\n      const em = await getEM()\n      const session = await em.findOne(\n        SessionModel,\n        { sessionToken },\n        { populate: [\"user\"] }\n      )\n      if (!session || !session.user) return null\n\n      return {\n        user: wrap(session.user).toObject(),\n        session: wrap(session).toObject(),\n      }\n    },","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/adapter-mikro-orm/src/index.ts#L141-L177","documentation":"unlinkAccount looks up the AccountModel by the composite { provider, providerAccountId } and throws 'Account not found' if no row matches, because there is nothing to remove. This adapter requires the account to exist; some other adapters just resolve silently, so callers expecting a no-op will see this error.","triggerScenarios":"Calling unlinkAccount with a provider/providerAccountId pair that was never stored, after the account row was already deleted, or with swapped/mismatched key names (e.g., passing providerAccountId where provider is expected).","commonSituations":"Double-unlink from duplicated UI actions; account removed by a cleanup job but still referenced in the client; wrong composite keys after renaming fields; manual adapter calls with data shaped differently than the DB.","solutions":["Confirm the account row exists for that exact provider + providerAccountId pair before unlinking","Guard with a lookup (getSessionAndUser/getUser) or catch the error and treat as already-unlinked","Deduplicate the unlink action in UI/API to avoid double deletion","Check that provider and providerAccountId values match what was stored at link time (casing, ids)"],"exampleFix":"// before\nawait adapter.unlinkAccount({ provider: 'github', providerAccountId })\n// after\ntry {\n  await adapter.unlinkAccount({ provider: 'github', providerAccountId })\n} catch (e) {\n  if (!(e as Error).message.includes('Account not found')) throw e\n}","handlingStrategy":"try-catch","validationCode":"const user = await adapter.getUserByAccount({ provider, providerAccountId })\nif (!user) return // already unlinked\nawait adapter.unlinkAccount({ provider, providerAccountId })","typeGuard":"function hasCompositeKey(v: unknown): v is { provider: string; providerAccountId: string } {\n  return typeof v === 'object' && v !== null &&\n    typeof (v as any).provider === 'string' && typeof (v as any).providerAccountId === 'string'\n}","tryCatchPattern":"try {\n  await adapter.unlinkAccount({ provider, providerAccountId })\n} catch (e) {\n  if ((e as Error).message === 'Account not found') {\n    return // idempotent: treat as already removed\n  } else throw e\n}","preventionTips":["Make unlink idempotent in caller code","Check getUserByAccount before unlinking","Deduplicate unlink buttons/actions","Verify provider/providerAccountId values match stored data"],"tags":["mikro-orm","account-linking","unlink","auth"],"backgroundTag":"record-not-found","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}