更新记录
示例使用以下 Prisma 架构
model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
comments Comment[]
}
model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
content String
post Post? @relation(fields: [postID], references: [id])
postID String?
}
更新记录
要更新记录,只需使用 FindUnique 或 FindMany 查询字段,然后通过调用 .Update()
来链接它。
updated, err := client.Post.FindMany(
db.Post.Title.Equals("what up"),
).Update(
db.Post.Content.Set("new content"),
db.Post.Title.Set("new title"),
).Exec(ctx)
更新关系
必需关系
您可以像创建记录时一样设置关系。
updated, err := client.Comment.FindUnique(
db.Comment.ID.Equals("id"),
).Update(
db.Comment.Post.Link(
db.Post.ID.Equals(postID),
),
).Exec(ctx)
可选关系
对于可选关系,您还可以取消链接关系,以便将外键值设置为 NULL
updated, err := client.Comment.FindUnique(
db.Comment.ID.Equals("id"),
).Update(
db.Comment.Post.Unlink(),
).Exec(ctx)