Skip to content

修复登录报错:Cannot read properties of undefined (reading 'findUnique')#262

Merged
Crokily merged 1 commit intoInvolutionHell:mainfrom
Crokily:fix/Colin/schemaUpdate
Mar 4, 2026
Merged

修复登录报错:Cannot read properties of undefined (reading 'findUnique')#262
Crokily merged 1 commit intoInvolutionHell:mainfrom
Crokily:fix/Colin/schemaUpdate

Conversation

@Crokily
Copy link
Contributor

@Crokily Crokily commented Mar 4, 2026

问题描述

使用 GitHub OAuth 登录时,服务端抛出 AdapterError

[auth][error] AdapterError
[auth][cause]: TypeError: Cannot read properties of undefined (reading 'findUnique')
at getUserByAccount (...)

根因分析

通过追溯 git 历史,定位到问题的完整因果链:

  1. b245705(2025-09-21):初始创建 prisma/schema.prisma,模型使用 PascalCase 命名(UserAccountSession),符合 Auth.js 适配器规范。此时认证使用 @auth/neon-adapter,登录功能正常。

  2. af74c13(2025-10-01):"拉下新的数据库结构"——执行了 prisma db pull,PostgreSQL 的表名是小写的 accounts/users/sessions,导致模型名被覆盖为小写,同时关系定义(@relation)和唯一约束(@@unique)全部丢失。

  3. 2025-10 ~ 2026-02(约 5 个月):由于 @auth/neon-adapter 直接执行原生 SQL 查询,不依赖 Prisma Client 的模型访问器,因此模型命名错误未暴露,登录始终正常。

  4. 1d358b4(2026-02-20):"迁移 Prisma 到 7"——将认证适配器从 @auth/neon-adapter 切换为 @auth/prisma-adapter。Prisma Adapter 通过 prisma.account.findUnique() 访问数据,但由于模型名为小写 accounts,Prisma 生成的访问器是 prisma.accounts,而 prisma.accountundefined,触发报错。

修复方式

将 Prisma schema 中的认证相关模型恢复为 @auth/prisma-adapter 要求的 PascalCase 命名,并通过 @@map() 保持数据库表名不变:

  • accountsAccount@@map("accounts")
  • usersUser@@map("users")
  • sessionsSession@@map("sessions")
  • verification_tokenVerificationToken@@map("verification_token")

同时补回 db pull 丢失的关系定义和约束:

  • Account.userSession.user 关系字段(onDelete: Cascade
  • @@unique([provider, providerAccountId])
  • sessionToken @unique
  • email @unique

修复后本地自测可登录

@Crokily Crokily requested a review from longsizhuo March 4, 2026 10:42
@vercel
Copy link

vercel bot commented Mar 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
website-preview Ready Ready Preview, Comment Mar 4, 2026 10:46am

@vercel
Copy link

vercel bot commented Mar 4, 2026

@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.

@Crokily Crokily marked this pull request as ready for review March 4, 2026 10:43
@Crokily Crokily merged commit 6f30cad into InvolutionHell:main Mar 4, 2026
3 of 4 checks passed
@longsizhuo
Copy link
Member

这是我见过最牛逼的MR, 连怎么报错的都说出来了, 牛逼plus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants