Optional Unique ( String? @unique) not working in MongoDB
See original GitHub issueBug 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
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:
- Created a year ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, all good: https://github.com/prisma/prisma/issues/3076 or https://github.com/prisma/prisma/issues/6974
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.