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.

Optional Unique ( String? @unique) not working in MongoDB

See original GitHub issue

Bug description

MongoDB Partial Indexes not creating

ts-node --transpile-only ./src/index.ts

PrismaClientKnownRequestError:
Invalid `prisma.user.create()` invocation in
PS C:\Users\muhad.bk\Desktop\playground\userOnboard> npm run tests

> useronboard@1.0.0 tests
> ts-node --transpile-only ./src/test.ts

done first
PrismaClientKnownRequestError:
Invalid `prisma.user.create()` invocation in
C:\Users\muhad.bk\Desktop\playground\userOnboard\src\test.ts:17:23

 
→ 17 await prisma.user.create(
  Unique constraint failed on the constraint: `User_mobile_key`
    at Object.request (\playground\userOnboard\node_modules\@prisma\client\runtime\index.js:45578:15)
    at async PrismaClient._request (\playground\userOnboard\node_modules\@prisma\client\runtime\index.js:46405:18) {
  code: 'P2002',
  clientVersion: '3.12.0',
  meta: { target: 'User_mobile_key' }
}

How to reproduce

schema

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

index.ts

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
    await prisma.$connect();

    await prisma.user.create({
        data: {
            email: "test1@gmail.com",
        }
    });
    console.log("done first");
    await prisma.user.create({
        data: {
            email: "test2@gmail.com",
        }
    });
}

main().catch(console.log).finally(async () => {
    await prisma.$disconnect();
});

Expected behavior

MongoDB Partial Indexes

Prisma Doc

It will create two new records where the email is set to NULL in the database.

Prisma information

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

Environment & setup

  • OS: Windows
  • Database: Mongo DB
  • Node.js version: 16
  • Prisma 3.12

Prisma Version

3.12

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
dieeisenefaustcommented, Jun 3, 2022

Was doing some googling to make sure partial (and maybe wildcard?) indexes were on the roadmap. Glad to have found this issue.

@janpio - looks like you have slated this as an enhancement. From your comment above, I just wanted to make sure you didn’t still need someone to open a feature request issue. I can do that, if required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Unique index not working! - Stack Overflow
1 · Unique seems to work on one of my hosts, but fails to enforce uniques using exactly same node/mongoose code on a...
Read more >
Unique Indexes — MongoDB Manual
A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB...
Read more >
Hi all Optional Unique String unique not working in MongoDB
Hi all Optional Unique String unique not working in MongoDB any one have the same issue As per prisma doc It will create...
Read more >
Unique Field in MongoDB Document in Spring Data - Baeldung
With that configuration, indexes will be created on boot time if they do not yet exist. But, we have to remember that we...
Read more >
Understanding `unique` in Mongoose - Mastering JS
A common gotcha is that the unique option tells Mongoose to define a unique index. That means Mongoose does not check uniqueness when...
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