如何修复Prisma LinkAccountError:列值长度超过类型限制
问题:Create-T3-App中Next Auth + Prisma Adapter使用Google登录失败(Discord登录正常)
在Create-T3-App项目中,使用Next Auth搭配Prisma Adapter连接PlanetScale数据库,Discord登录可正常创建用户及关联账号,但Google登录时触发错误,无法创建账号,数据库已能正常写入其他数据。
错误日志
prisma:query BEGIN prisma:query INSERT INTO `foo`.`Account` (`id`,`userId`,`type`,`provider`,`providerAccountId`,`access_token`,`expires_at`,`token_type`,`scope`,`id_token`) VALUES (?,?,?,?,?,?,?,?,?,?) prisma:query ROLLBACK prisma:error Invalid `p.account.create()` invocation in ~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42 16 }, 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }), 18 deleteUser: (id) => p.user.delete({ where: { id } }), → 19 linkAccount: (data) => p.account.create( The provided value for the column is too long for the column's type. Column: for [next-auth][error][adapter_error_linkAccount] https://next-auth.js.org/errors#adapter_error_linkaccount Invalid `p.account.create()` invocation in ~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42 16 }, 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }), 18 deleteUser: (id) => p.user.delete({ where: { id } }), → 19 linkAccount: (data) => p.account.create( The provided value for the column is too long for the column's type. Column: for { message: '\n' + 'Invalid `p.account.create()` invocation in\n' + '~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42\n' + '\n' + ' 16 },\n' + ' 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }),\n' + ' 18 deleteUser: (id) => p.user.delete({ where: { id } }),\n' + '→ 19 linkAccount: (data) => p.account.create(\n' + "The provided value for the column is too long for the column's type. Column: for", stack: 'Error: \n' + 'Invalid `p.account.create()` invocation in\n' + '~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42\n' + '\n' + ' 16 },\n' + ' 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }),\n' + ' 18 deleteUser: (id) => p.user.delete({ where: { id } }),\n' + '→ 19 linkAccount: (data) => p.account.create(\n' + "The provided value for the column is too long for the column's type. Column: for\n" + ' at fn.handleRequestError (~/node_modules/@prisma/client/runtime/library.js:174:6477)\n' + ' at fn.handleAndLogRequestError (~/node_modules/@prisma/client/runtime/library.js:174:5907)\n' + ' at fn.request (~/node_modules/@prisma/client/runtime/library.js:174:5786)\n' + ' at async t._request (~/node_modules/@prisma/client/runtime/library.js:177:10477)', name: 'Error' } [next-auth][error][OAUTH_CALLBACK_HANDLER_ERROR] https://next-auth.js.org/errors#oauth_callback_handler_error Invalid `p.account.create()` invocation in ~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42 16 }, 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }), 18 deleteUser: (id) => p.user.delete({ where: { id } }), → 19 linkAccount: (data) => p.account.create( The provided value for the column is too long for the column's type. Column: for Error: Invalid `p.account.create()` invocation in ~/node_modules/@next-auth/prisma-adapter/dist/index.js:19:42 16 }, 17 updateUser: ({ id, ...data }) => p.user.update({ where: { id }, data }), 18 deleteUser: (id) => p.user.delete({ where: { id } }), → 19 linkAccount: (data) => p.account.create( The provided value for the column is too long for the column's type. Column: for at fn.handleRequestError (~/node_modules/@prisma/client/runtime/library.js:174:6477) at fn.handleAndLogRequestError (~/node_modules/@prisma/client/runtime/library.js:174:5907) at fn.request (~/node_modules/@prisma/client/runtime/library.js:174:5786) at async t._request (~/node_modules/@prisma/client/runtime/library.js:177:10477) { name: 'LinkAccountError', code: 'P2000' }
解决方案
错误代码P2000表示字段值长度超过数据库列定义的上限,Google OAuth返回的id_token(或其他字段)长度远大于Discord的响应,超出了默认String类型(MySQL默认255字符)的限制。解决步骤如下:
- 打开项目中的
prisma/schema.prisma文件 - 找到
Account模型,将可能过长的字段(重点是id_token,也可同步调整scope)修改为Text类型:
model Account { id String @id @default(cuid()) userId String type String provider String providerAccountId String refresh_token String? @db.Text access_token String? @db.Text expires_at Int? token_type String? scope String? @db.Text id_token String? @db.Text // 关键修改:将id_token改为Text类型 session_state String? user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@unique([provider, providerAccountId]) }
- 执行命令同步数据库变更(PlanetScale推荐使用push而非migrate):
npx prisma db push
- 重启项目后,测试Google登录即可正常创建账号。
内容的提问来源于stack exchange,提问作者circular_reference
相关产品推荐
相关产品推荐

