PrismaP
Prisma2y ago
19 replies
Nik

@unique on optional field

model Player {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  steam_id  String    @unique
  ...
  user      User?     @relation(fields: [user_id], references: [id])
  user_id   String?    @db.ObjectId @unique
  ...
  @@map("players")
}


and


model User {
  id            String    @id @default(auto()) @map("_id") @db.ObjectId
  name          String?
  email         String?   @unique
  ...
  player        Player?
  ...
  @@map("users")
}


As you can see, user_id is an optional field. However the relation REQUIRES it to be marked as unique. In doing so, I get the following error: duplicate key { user_id: null }

I imagine this will be an issue for excluded emails too, though I haven't run into it yet
Was this page helpful?