MongoDB Index Management
See original GitHub issueProblem
Right now indices aren’t enforced in the database. You may add an index to a field or model in the Prisma Schema Language, but it won’t be enforced unless you manually create the index outside of Prisma.
Where this comes up most often is with @unique
attributes in the Prisma Schema Language. If you don’t manually create a unique index in MongoDB (using a tool like Compass), there’s nothing stopping you from inserting duplicate records into your MongoDB database, despite what your schema says. Furthermore, the Prisma Client throws exceptions if you’ve labeled a column as unique but it’s not actually unique.
- People work around this by adding unique indices outside of Prisma and then writing
@unique
by hand to enable certain API features likefindUnique
in the client - With Mongoose, you can create unique indexes at runtime when the schema is initialized. There’s some pitfalls with creating an index on boot, so they also offer a way to disable this.
- From my understanding, people usually run this once with Mongoose then maybe comment it out or disable it once you have the index setup .
Suggested solution
Ideally prisma db push
is able to manage unique constraints and indexes.
My hope is that this can be split off from the larger topic of #7305 because this has a more immediate need and should be less complicated.
Additional context
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top GitHub Comments
Hi @SantiagoPaak Could you open a new issue and provide the information needed for a reproduction? If you ran
prisma db push
the index should have been created 🤔Ah, my mistake then. Didn’t knew I had to do that. Works correctly then!