[英]Prisma and mongodb optional unique field throws "Unique constraint" error when empty
我在棱鏡模式中有以下模型:
model Order {
id String @id @default(auto()) @map("_id") @db.ObjectId
customer User @relation(fields: [customerId], references: [id])
customerId String @db.ObjectId
products Json
status String @default("pending")
paymentMethod String?
pixPayment Payment? @relation(name: "pixPayment", fields: [pixPaymentId], references: [id])
pixPaymentId String? @unique @db.ObjectId
creditCardPayment Payment? @relation(name: "creditCardPayment", fields: [creditCardPaymentId], references: [id])
creditCardPaymentId String? @unique @db.ObjectId
boletoPayment Payment? @relation(name: "boletoPayment", fields: [boletoPaymentId], references: [id])
boletoPaymentId String? @unique @db.ObjectId
total Float
createdAt DateTime @default(now())
@@map("Orders")
}
model Payment {
id String @id @default(auto()) @map("_id") @db.ObjectId
paymentId Int
amount Float
paymentMethod String
customer User @relation(fields: [customerId], references: [id])
customerId String @db.ObjectId
payer Json?
installments Int @default(1)
status String @default("pending")
dateOfExpiration DateTime
dateApproved DateTime?
barcode String?
boletoUrl String?
pixQrCode String?
pixQrCodeBase64 String?
lastFourDigitsCard String?
cardHolder Json?
createdAt DateTime @default(now())
pixPaymentOrder Order? @relation("pixPayment")
creditCardPaymentOrder Order? @relation("creditCardPayment")
boletoPaymentOrder Order? @relation("boletoPayment")
@@map("Payments")
}
我正在嘗試創建一個包含必填字段(產品、總計、customerId)的訂單文檔,但只有一次,如果我嘗試創建另一個訂單,我會收到錯誤消息:“Unique constraint failed on the constraint: Orders_pixPaymentId_key
”。 pixPaymentId 是一個唯一的可選字段,在這種情況下我不會傳遞它。 訂單創建代碼:
const order = await prisma.order.create({
data: {
products,
total,
customerId: userId
},
select: {
id: true
}
});
我原以為可以在沒有 pixPaymentId 的情況下創建多個訂單文檔,因為它是一個可選字段,但我收到了唯一約束錯誤。
Prisma 尚不支持unique and nullable
特性。
對於這種情況,我們只能將其聲明為nullable
並手動處理唯一性。 實施unique and soft delete
時會出現同樣的問題。
他們不支持它的原因是因為大多數數據庫在唯一約束上拒絕重復的 NULL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.