![](/img/trans.png)
[英]Updating useContext after adding new value to an object in the array
[英]Prisma Schema not updating properly after adding new fields
正如標題所述,我在 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[]
}
我在用戶 model 上添加了email
字段,並將groupId
和activityId
字段更改為可選。 我還將role
字段的類型更改為String
。 我運行prisma migrate
和prisma 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'
}
看起來該操作正在使用以前版本的模式,看到它如何說 email 字段不存在並且role
字段不是字符串類型。 此外, groupId
和activityId
字段仍按要求顯示。 我不知道是否有某種緩存。 我已經嘗試刪除所有遷移並從頭開始重新部署所有內容。 我什至刪除了 heroku 中的數據庫並重新開始,但我仍然遇到同樣的錯誤。
運行npm install
(無需刪除 node_modules)然后重新生成 Prisma 類型可以解決此問題。
由於npm i
刪除了舊的 Prisma 代, npx npx prisma generate
必須從您的schema.prisma
生成新的。
Ryan 關於為 Prisma 添加安裝后腳本的評論也是一個不錯的 QOL 改進。
我有更接近這個問題的東西。 在我的例子中,我用新模型更新了我的 Schema.prisma,但是每當我運行“prisma migrate dev”時,它都不會更新。 事實證明,錯誤是因為在運行代碼之前進行更改后我沒有點擊“保存”。 (在我的辯護中,我認為自動保存可以解決這個問題,但沒有)。
在運行“prisma migrate dev”之前再次嘗試保存文件。
您可以在migrations.sql文件中更新它並運行prisma migrate dev 。 那會工作的。
在運行yarn prisma migrate dev后重新加載 vscode window 對我來說效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.