简体   繁体   中英

Prisma and mongodb optional unique field throws "Unique constraint" error when empty

I have the following models in prisma schema:

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")
}

I'm trying to create an Order document with the required fields (products, total, customerId), it works but only once, if I try to create another Order I get the error: "Unique constraint failed on the constraint: Orders_pixPaymentId_key ". The pixPaymentId is an unique and optional field and in this case I'm not passing it. Order create code:

const order = await prisma.order.create({
            data: {
                products,
                total,
                customerId: userId
            },
            select: {
                id: true
            }
        });

I was expecting that would be possible to create multiple Orders documents without pixPaymentId as it is a OPTIONAL field, but I'm getting the Unique constraint error.

Prisma doesn't support unique and nullable feature yet.

For this case, we can only declare it as nullable and handle uniqueness manually. Same problem will occur when implementing unique and soft delete .

The reason why they don't support it because most database reject duplicate NULLs on unique constraint.

Follow this discussion: https://github.com/prisma/prisma/issues/3387

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM