17

As the title states, I am using Prisma 2 in a Next JS app. I have a very simple schema:

  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[]
}

I added the email field on the User model and changed the groupId and activityId fields to be optional. I also changed the type for the role field to String. I run prisma migrate and prisma up to create a new migration and sync the database (using a remote heroku postgresql database as my datasource) and everything runs fine. No errors. However, when I try to create a new User, I get the following error:

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'
}

It seems like the operation is using a previous version of the schema seeing as how it says that the email field does not exist and the role field is not a String type. Also, the groupId and activityId fields are still shown as required. I don't know if there some sort of cache. I already tried deleting all migrations and redeploying everything from scratch. I even deleted the database in heroku and started fresh but I am still getting the same error.

Isaac
  • 171
  • 1
  • 1
  • 7
  • 7
    I would suggest adding `prisma generate` as a `postinstall` script in your `package.json` so that the latest PrismaClient is always generated. A same problem was faced by a user [here](https://github.com/prisma/prisma/discussions/4901) and this script solved it. – Ryan Jan 11 '21 at 09:57
  • Ok. I will look into it. I see that in that part of the solution was also updating to version 2.14. However, I had to roll back to 2.12 because of an ongoing [issue](https://github.com/prisma/prisma/issues/4751) where a shadow database cannot be created when using a cloud based database in Heroku which is precisely my case. – Isaac Jan 11 '21 at 16:22
  • 2
    Ok so I tried running `prisma generate` and it worked after restarting my local server of course. Seems I just had to read the [documentation](https://www.prisma.io/docs/concepts/components/prisma-client/generating-prisma-client) more closely. You need to run this command every time you make a change to your schema. Thanks a lot for the help! – Isaac Jan 11 '21 at 16:41

8 Answers8

28

To fix this in VS Code after running npx prisma migrate dev or npx prisma db push, you can try one of these following methods:

  • Reloading VS Code (Just simply close and reopen VS Code)
  • Restart VS Code language server (Hit Ctrl + Shift + P, then search for Restart TS server)

Two method above will take a few minute to get VS Code working again, so I recommend this way:

  • Open file node_modules\.prisma\client\index.d.ts to get VS Code re-indexing this file (because it's too large, VS Code does not reload this file ), then it'll work in few seconds.
Hoàng Huy Khánh
  • 1,071
  • 8
  • 16
Gustavo Ribeiro
  • 393
  • 3
  • 5
16

Running an npm install (no need to remove node_modules) and then re generating the Prisma types can fix this issue.

Since npm i removes the old Prisma generations, npx prisma generate will have to generate new ones from your schema.prisma.

Ryan's comment about adding a post install script for Prisma is a nice QOL improvement, too.

Edit: Closing and opening your editor (in my case VsCode) will fix the red line errors. I think the extension struggles to update itself with the new changes. It's a pain but does work if the other solutions don't.

James
  • 2,516
  • 2
  • 19
  • 31
  • 1
    Even deleting node_modules doesn't seem to help me :/ – Powersource Nov 12 '21 at 18:29
  • Have added an edit which may help. It's not a fun solution but it works until the extension is patched – James Nov 23 '21 at 11:54
  • I think the bug for me in the end turned out to be something seemingly unrelated, something like using connectOrCreate inside it wrong, I think maybe passing an undefined to it in a place where it didn't expect it. So maybe just a really bad error message. – Powersource Nov 23 '21 at 15:21
  • Deleting `node_modules` and running `npm install` and then `npx prisma generate` working for me. – Cathal Mac Donnacha Sep 14 '22 at 21:52
1

I had something closer to this problem. In my case, I updated my Schema.prisma with new models but anytime I run "prisma migrate dev", it wouldn't update. It turns out the error was because I didn't hit "Save" after making the changes before running the code. (In my defence, I thought auto-save took care of that but no).

Try saving the file again before you run "prisma migrate dev".

Martin Oputa
  • 349
  • 4
  • 6
1

After running npx prisma db push you must restart VS Code for the changes to show up.

1

Restarting vs code did the trick for me XD

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34498632) – Wahlstrommm Jun 07 '23 at 11:39
  • @Wahlstrommm Reloading VSCode does fix the problem as op answered. – stuckoverflow Jun 20 '23 at 06:26
1

Delete .next in the root directory and restart the dev server.

0

you can update it in migrations.sql file and run prisma migrate dev. that'll work.

0

In my case, the problem occurred because I copied the skeleton of the path from another file and did not change the name of the table that I had to access, that's why it sent me this error.

router.post('/routes', async (req, res) => {
  const newData = await prisma.nameTable.create({
    data: req.body,
  });
  res.json(newData);
});
Charlie
  • 1
  • 1