使用Next-Auth+Prisma+MongoDB第三方登录遇LinkAccountError求助
解决NextAuth + Prisma + MongoDB第三方登录时
user_id字段错误问题 错误原因
从报错信息可以看出,@next-auth/prisma-adapter在创建Account记录时,同时传入了user_id和userId两个字段,但你的Prisma Schema中Account模型仅支持驼峰格式的userId参数。这是因为NextAuth核心包与Prisma Adapter版本不兼容:旧版Adapter会输出下划线分隔的字段名(如user_id),而新版则使用驼峰式命名,与你的Schema定义匹配。
解决方案
统一NextAuth与Adapter版本
确保next-auth和@next-auth/prisma-adapter版本完全一致,执行以下命令更新到最新兼容版本:# npm npm install next-auth@latest @next-auth/prisma-adapter@latest # yarn yarn add next-auth@latest @next-auth/prisma-adapter@latest重新生成Prisma Client
更新依赖后,必须重新生成适配当前Schema的Prisma Client:npx prisma generate验证Schema配置(你的现有配置已正确,可跳过此步)
确认Account模型的关联字段为驼峰式userId,且与User模型正确关联:model Account { id String @id @default(auto()) @map("_id") @db.ObjectId userId String @db.ObjectId // 此处为驼峰式,正确 type String provider String providerAccountId String @map("provider_account_id") // ... 其他字段 user User @relation(fields: [userId], references: [id], onDelete: Cascade) }清理缓存并重启服务
如果问题仍存在,清理依赖缓存后重新安装:# npm rm -rf node_modules package-lock.json npm install # yarn rm -rf node_modules yarn.lock yarn install之后重启开发服务器,再次尝试第三方登录。
内容的提问来源于stack exchange,提问作者Velislav Dimitrov
相关产品推荐
相关产品推荐

