reactjs - 添加新字段后,Prisma Schema 未正确更新

标签 reactjs next.js heroku-postgres prisma prisma2

正如标题所述,我在 Next JS 应用程序中使用 Prisma 2。我有一个非常简单的架构:

  model User {
  id             Int       @id @default(autoincrement())
  firstName      String
  middleName     String?
  firstLastname  String
  secondLastname String?
  email          String
  role           String
  group          Group?    @relation(fields: [groupId], references: [id])
  groupId        Int?
  activity       Activity? @relation(fields: [activityId], references: [id])
  activityId     Int?
  createdOn      DateTime  @default(now())
  updatedOn      DateTime  @default(now())
}

model JobTitle {
  id        Int      @id @default(autoincrement())
  name      String
  createdOn DateTime @default(now())
  updatedOn DateTime @default(now())
}

model Group {
  id        Int      @id @default(autoincrement())
  name      String
  users     User[]
  createdOn DateTime @default(now())
  updatedOn DateTime @default(now())
}

model Activity {
  id    Int    @id @default(autoincrement())
  name  String
  users User[]
}
我添加了 email User 模型上的字段并更改了 groupIdactivityId字段是可选的。我还更改了 role 的类型字段到 String .我跑 prisma migrateprisma up创建一个新的迁移并同步数据库(使用远程 heroku postgresql 数据库作为我的数据源)并且一切正常。没有错误。但是,当我尝试创建新用户时,出现以下错误:
An error ocurred:  PrismaClientValidationError:
Invalid `prisma.user.create()` invocation:

{
  data: {
    firstName: 'John',
    middleName: 'Edgar',
    firstLastname: 'Doe',
    secondLastname: 'Smith',
    email: 'john@email.com',
    ~~~~~
    role: 'ADMIN',
          ~~~~~~~
+   group: {
+     create?: GroupCreateWithoutUsersInput,
+     connect?: GroupWhereUniqueInput,
+     connectOrCreate?: GroupCreateOrConnectWithoutusersInput
+   },
+   activity: {
+     create?: ActivityCreateWithoutUsersInput,
+     connect?: ActivityWhereUniqueInput,
+     connectOrCreate?: ActivityCreateOrConnectWithoutusersInput
+   },
?   createdOn?: DateTime,
?   updatedOn?: DateTime
  }
}

Unknown arg `email` in data.email for type UserCreateInput. Did you mean `role`?
Argument role: Got invalid value 'ADMIN' on prisma.createOneUser. Provided String, expected RoleCreateOneWithoutUserInput:
type RoleCreateOneWithoutUserInput {
  create?: RoleCreateWithoutUserInput
  connect?: RoleWhereUniqueInput
  connectOrCreate?: RoleCreateOrConnectWithoutUserInput
}
Argument group for data.group is missing.
Argument activity for data.activity is missing.

Note: Lines with + are required, lines with ? are optional.

    at Document.validate (C:\Users\user\Documents\projects\employee-evaluation-app\employee-evaluation-app\node_modules\@prisma\client\runtime\index.js:77411:19)
    at NewPrismaClient._executeRequest (C:\Users\user\Documents\projects\employee-evaluation-app\employee-evaluation-app\node_modules\@prisma\client\runtime\index.js:79063:17)
    at C:\Users\user\Documents\projects\employee-evaluation-app\employee-evaluation-app\node_modules\@prisma\client\runtime\index.js:79000:52
    at AsyncResource.runInAsyncScope (node:async_hooks:197:9)
    at NewPrismaClient._request (C:\Users\user\Documents\projects\employee-evaluation-app\employee-evaluation-app\node_modules\@prisma\client\runtime\index.js:79000:25)
    at Object.then (C:\Users\user\Documents\projects\employee-evaluation-app\employee-evaluation-app\node_modules\@prisma\client\runtime\index.js:79117:39)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:93:5) {
  clientVersion: '2.12.0'
}
该操作似乎正在使用先前版本的架构,因为它说电子邮件字段不存在和 role字段不是字符串类型。另外,groupIdactivityId字段仍按要求显示。我不知道是否有某种缓存。我已经尝试删除所有迁移并从头开始重新部署所有内容。我什至删除了 heroku 中的数据库并重新开始,但我仍然遇到同样的错误。

最佳答案

运行 npm install (不需要删除 node_modules)然后重新生成 Prisma 类型可以解决这个问题。
npm i删除旧的 Prisma 代,npx prisma generate必须从您的 schema.prisma 生成新的.
Ryan 关于为 Prisma 添加安装后脚本的评论也是一个很好的 QOL 改进。
编辑:
关闭和打开你的编辑器(在我的例子中是 VsCode)将修复红线错误。我认为扩展很难通过新的变化来更新自己。这很痛苦,但如果其他解决方案不起作用,它确实有效。

关于reactjs - 添加新字段后,Prisma Schema 未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65663292/

相关文章:

reactjs - 在nextjs中获取URL路径名

node.js - 无法加载资源: the server responded with a status of 431 (Request Header Fields Too Large)

reactjs - Redux - 在 TextInput 组件中输入内容后状态无法正确更新

css - 如何在 React 组件中应用渐变动画效果

database - 如何使用 sql 文件填充 heroku postgresql 数据库

heroku-postgres - 无法通过 heroku pg :psql (no response) [Windows] 连接

heroku - 以编程方式更改 heroku dataclips 的数据库

javascript - 不要使用 React-Select 清除选择时的输入

node.js - MongoDB Node.js 驱动程序 4.0.0 : Cursor session id issues in production on Vercel

next.js - NextJS 13 Beta 应用程序目录 API 与 Prisma