'Prisma Schema not updating properly after adding new fields
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: '[email protected]',
~~~~~
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.
Solution 1:[1]
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.
Solution 2:[2]
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".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Martin Oputa |