假設我有這個架構:
model User {
id String @id @default(cuid())
name String
email String
profile Profile?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Profile {
id Int @id @default(autoicrement())
bio String?
avatar String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
}
現在我要存檔的是,如果更新了用戶的個人資料,例如更新了一個bio欄位,我希望模型上的updatedAt欄位User自動反映這一點,并更新為當前時間戳。
任何指南、提示或建議將不勝感激!!
uj5u.com熱心網友回復:
最簡單的方法是制作一些我認為的包裝函式,例如:
const updateUserProfile = (id, data) => {
return prisma.profile.update({
where: {
id
},
data: {
...data,
user: {
update: {
updatedAt: new Date()
}
}
}
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/418432.html
標籤:
上一篇:如何上傳圖片云?
