修复登录报错:Cannot read properties of undefined (reading 'findUnique')#262
Merged
Crokily merged 1 commit intoInvolutionHell:mainfrom Mar 4, 2026
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@Crokily is attempting to deploy a commit to the longsizhuo's projects Team on Vercel. A member of the Team first needs to authorize it. |
Member
|
这是我见过最牛逼的MR, 连怎么报错的都说出来了, 牛逼plus |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
使用 GitHub OAuth 登录时,服务端抛出
AdapterError:[auth][error] AdapterError
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'findUnique')
at getUserByAccount (...)
根因分析
通过追溯 git 历史,定位到问题的完整因果链:
b245705(2025-09-21):初始创建prisma/schema.prisma,模型使用 PascalCase 命名(User、Account、Session),符合 Auth.js 适配器规范。此时认证使用@auth/neon-adapter,登录功能正常。af74c13(2025-10-01):"拉下新的数据库结构"——执行了prisma db pull,PostgreSQL 的表名是小写的accounts/users/sessions,导致模型名被覆盖为小写,同时关系定义(@relation)和唯一约束(@@unique)全部丢失。2025-10 ~ 2026-02(约 5 个月):由于
@auth/neon-adapter直接执行原生 SQL 查询,不依赖 Prisma Client 的模型访问器,因此模型命名错误未暴露,登录始终正常。1d358b4(2026-02-20):"迁移 Prisma 到 7"——将认证适配器从@auth/neon-adapter切换为@auth/prisma-adapter。Prisma Adapter 通过prisma.account.findUnique()访问数据,但由于模型名为小写accounts,Prisma 生成的访问器是prisma.accounts,而prisma.account为undefined,触发报错。修复方式
将 Prisma schema 中的认证相关模型恢复为
@auth/prisma-adapter要求的 PascalCase 命名,并通过@@map()保持数据库表名不变:accounts→Account(@@map("accounts"))users→User(@@map("users"))sessions→Session(@@map("sessions"))verification_token→VerificationToken(@@map("verification_token"))同时补回
db pull丢失的关系定义和约束:Account.user、Session.user关系字段(onDelete: Cascade)@@unique([provider, providerAccountId])sessionToken @uniqueemail @unique修复后本地自测可登录