question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoDB Index Management

See original GitHub issue

Problem

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 like findUnique 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Jolg42commented, Sep 28, 2022

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 🤔

1reaction
SantiagoPaakcommented, Sep 30, 2022

Ah, my mistake then. Didn’t knew I had to do that. Works correctly then!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage Indexes — MongoDB Manual
This page shows how to manage existing indexes. For instructions on creating indexes, refer to the specific index type pages.
Read more >
Manage Indexes — MongoDB Manual
The following sections provide methods for viewing existing indexes on a collection or an entire database.
Read more >
Index Management — Mongoid Manual 8.0 - MongoDB
Specifying Indexes¶. You can define indexes on documents using the index macro. Provide the key for the index along with a direction.
Read more >
Indexing Strategies — MongoDB Manual
Generally, MongoDB only uses one index to fulfill most queries. However, each clause of an $or query may use a different index, and...
Read more >
MongoDB Index | A Beginners Guide
Learn what indexes are in MongoDB, from the best practices for delivering performance at scale to the tools helping you ... MongoDB Atlas...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found