[英]Prisma delete many to many relationship with Composite Key
我在這里有這個架構:
model label {
title String @id @db.VarChar(16)
color String @db.VarChar(16)
labelplaylist labelplaylist[]
}
model labelplaylist {
playlistId Int
labelId String @db.VarChar(16)
label label @relation(fields: [labelId], references: [title])
playlist playlist @relation(fields: [playlistId], references: [id])
@@id([playlistId, labelId])
@@index([labelId], name: "labelId")
}
model playlist {
id Int @id @default(autoincrement())
createdAt DateTime? @default(now()) @db.DateTime(0)
title String @db.VarChar(100)
labelplaylist labelplaylist[]
@@index([userId], name: "userId")
}
我只想刪除標簽和播放列表之間的關系。 我試過這樣做:
const deleteRelation = await prisma.labelplaylist.delete({
where: {
playlistId_labelId:
},
})
我有標簽和播放列表表的主鍵,但我不知道如何獲得主鍵 => playlistId_labelId。
謝謝幫忙。
這是帶有復合鍵的查詢的語法
const deleteRelation = await prisma.labelplaylist.delete({
where: {
playlistId_labelId: {
playlistId: playListIdVariable, //replace with appropriate variable
labelId: labelIdVariable, //replace with appropriate variable
},
},
});
您可以在 Prisma 文檔的CRUD 參考指南的按復合 ID 或復合唯一標識符獲取記錄小節中閱讀更多內容。 此參考顯示讀取數據,但刪除和更新的where
條件也類似。
由於它是一個顯式的多對多關系,嵌套的deleteMany
只刪除關系表記錄,就像一個disconnect
。 然后,您可以像這樣編寫查詢:
await req.db.playlist.update({
data: {
labels: {
deleteMany: {},
},
},
where: {
id: labelId,
},
})
或者反過來。
它不會刪除您的相關表記錄,而只會刪除它們之間的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.