Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Prisma not supporting optional @unique indexes in MongoDB #23870

Closed
Nik-Novak opened this issue Apr 16, 2024 · 2 comments
Closed

[BUG] Prisma not supporting optional @unique indexes in MongoDB #23870

Nik-Novak opened this issue Apr 16, 2024 · 2 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: indexes topic: mongodb topic: prisma db push CLI: prisma db push topic: (@)@unique

Comments

@Nik-Novak
Copy link

Bug description

** THE PROBLEM**
OPTIONAL @unique indexes are erroring with duplicate null/undefined values.
This means that two records that omit an optionally @unique field will clash

THE SOLUTION
What Prisma should be doing is setting Sparse = true on any index of an optional @unique field
But I am having to manually remember to do this for every index from an optional @unique

If we mark an OPTIONAL field foo @unique, run:
db.collection.createIndex( { foo: 1 }, { unique:true, sparse: true } )

If we mark a REQUIRED field foo @unique, run:
db.collection.createIndex( { foo: 1 }, { unique:true )

^ This is all prisma would have to do, currently it runs the second in both cases

This is how I'm manually editing the indexes created by Prisma:

image

How to reproduce

Expected behavior

No response

Prisma information

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")
}
model User {
  id            String    @id @default(auto()) @map("_id") @db.ObjectId
  name          String?
  email         String?   @unique
  ...
  player        Player?
  ...
  @@map("users")
}
//create two players and don't assign them to a User
database.player.create( ... )
database.player.create ( ... )
//commandline
prisma db push (FAILS)

Environment & setup

  • OS: Debian/Windows
  • Database: MongoDB
  • Node.js version: 20.12.0

Prisma Version

5.11.0
@Nik-Novak Nik-Novak added the kind/bug A reported bug. label Apr 16, 2024
@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: indexes team/schema Issue for team Schema. topic: prisma db push CLI: prisma db push topic: mongodb topic: (@)@unique labels Apr 18, 2024
@janpio
Copy link
Member

janpio commented Apr 19, 2024

Prisma currently does not support sparse unique indexes. There is a feature request tracking them here: #3419 Would that solve your problem if/when we implement this @Nik-Novak?

@Nik-Novak
Copy link
Author

Prisma currently does not support sparse unique indexes. There is a feature request tracking them here: #3419 Would that solve your problem if/when we implement this @Nik-Novak?

Absolutely

@janpio janpio closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: indexes topic: mongodb topic: prisma db push CLI: prisma db push topic: (@)@unique
Projects
None yet
Development

No branches or pull requests

3 participants