{"record":{"id":"ffc56843303a64d0","repo":"nextauthjs/next-auth","slug":"couldn-t-create-session","errorCode":null,"errorMessage":"Couldn't create session","messagePattern":"Couldn't create session","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/adapter-d1/src/index.ts","lineNumber":274,"sourceCode":"      await deleteRecord(\n        db,\n        DELETE_ACCOUNT_BY_PROVIDER_AND_PROVIDER_ACCOUNT_ID_SQL,\n        [provider, providerAccountId]\n      )\n    },\n    async createSession({ sessionToken, userId, expires }) {\n      const id = crypto.randomUUID()\n      const createBindings = [id, sessionToken, userId, expires.toISOString()]\n      const getBindings = [sessionToken]\n      const session = await createRecord<AdapterSession>(\n        db,\n        CREATE_SESSION_SQL,\n        createBindings,\n        GET_SESSION_BY_TOKEN_SQL,\n        getBindings\n      )\n      if (session) return session\n      throw new Error(`Couldn't create session`)\n    },\n    async getSessionAndUser(sessionToken) {\n      const session: any = await getRecord<AdapterSession>(\n        db,\n        GET_SESSION_BY_TOKEN_SQL,\n        [sessionToken]\n      )\n      if (session === null) return null\n\n      const user = await getRecord<AdapterUser>(db, GET_USER_BY_ID_SQL, [\n        session.userId,\n      ])\n      if (user === null) return null\n\n      return { session, user }\n    },\n    async updateSession({ sessionToken, expires }) {\n      if (expires === undefined) {","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/adapter-d1/src/index.ts#L256-L292","documentation":"createSession inserts a session row and immediately re-selects it by session token in the same batch; if the SELECT yields nothing the adapter throws this error. The insert did not produce a readable session, so no session can be returned to the auth core.","triggerScenarios":"adapter.createSession({ sessionToken, userId, expires }) when the INSERT fails or the follow-up GET_SESSION_BY_TOKEN_SQL returns no row — sessions table missing, userId referencing a non-existent user, or schema mismatch.","commonSituations":"Sessions table never migrated, foreign-key mismatch on userId, custom session table names, expired/invalid expires value rejected by constraints, or local miniflare D1 batch quirks.","solutions":["Run the adapter's migration SQL so sessions (and accounts/users) tables exist with the expected columns.","Verify the userId passed exists in the users table before creating a session.","Check the D1 binding points at the same database used for users.","Update the adapter if you rely on batch insert+select on local D1/miniflare."],"exampleFix":"// before\nawait adapter.createSession({ sessionToken, userId: unknownId, expires })\n// after\nconst user = await adapter.getUser(unknownId)\nif (!user) throw new Error('cannot create session for missing user')\nawait adapter.createSession({ sessionToken, userId: unknownId, expires })","handlingStrategy":"validation","validationCode":"const user = await adapter.getUser(sessionData.userId)\nif (!user) throw new Error('cannot create session: userId does not exist')","typeGuard":"function isCreateSessionInput(s: unknown): s is AdapterSession {\n  return !!s && typeof s === 'object' && typeof (s as AdapterSession).sessionToken === 'string' && (s as AdapterSession).expires instanceof Date\n}","tryCatchPattern":"try {\n  const session = await adapter.createSession({ sessionToken, userId, expires })\n} catch (e) {\n  if (e instanceof Error && e.message === \"Couldn't create session\") {\n    // check sessions table exists and userId is valid, then retry or re-authenticate\n  }\n  throw e\n}","preventionTips":["Run migrations so the sessions table exists","Validate userId refers to an existing user before creating sessions","Use consistent expires values (Date objects) as the adapter expects","Test session flows on the same D1 environment you deploy to"],"tags":["database","d1","session","cloudflare"],"backgroundTag":"record-not-found-after-write","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}