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: @unique constraint not working

See original GitHub issue

Bug description

How to reproduce

Create a schema with @unique and add two objects with non-unique values.

Expected behavior

Throw an error

Prisma information

Environment & setup

  • OS: Mac OS
  • Database: MongoDB (using Azure CosmosDB with Mongo API)
  • Node.js version: 15.11.0
  • Prisma version: 2.21.2
prisma               : 2.21.2
@prisma/client       : 2.21.2
Current platform     : darwin
Query Engine         : query-engine e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt e421996c87d5f3c8f7eeadd502d4ad402c89464d (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : e421996c87d5f3c8f7eeadd502d4ad402c89464d
Studio               : 0.371.0
Preview Features     : mongodb

Schema & code

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongodb"]
}

model User {
  id        String   @id @default(dbgenerated()) @map("_id") @db.ObjectId
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email     String   @unique
}
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  await prisma.$connect();
  await prisma.user.create({
    data: {
      email: "test@example.com",
    },
  });
  await prisma.user.create({
    data: {
      email: "test@example.com",
    },
  });
}

main()
  .catch(console.error)
  .finally(() => prisma.$disconnect());

Result

➜  prismamongo npx ts-node index.ts
[
  {
    id: '6081b778001e4db9009f1569',
    createdAt: 2021-04-22T17:50:48.444Z,
    updatedAt: 2021-04-22T17:50:48.445Z,
    email: 'test@example.com'
  },
  {
    id: '6081b779001e4db9009f156a',
    createdAt: 2021-04-22T17:50:49.237Z,
    updatedAt: 2021-04-22T17:50:49.238Z,
    email: 'test@example.com'
  }
]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:20
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

61reactions
janpiocommented, Apr 14, 2022

Did you run npx prisma db push to create the index on the database @SLTN98?

10reactions
mustafat0kcommented, Apr 10, 2022

here we go again

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Unique index not working! - Stack Overflow
MongoDB cannot create a unique index on the specified index field(s ) if the collection already contains data that would violate the unique...
Read more >
Mongoose Unique Not Working - DEV Community ‍ ‍
but it doesn't work! , here are some reasons because which this wouldn't work. 1. Duplicate documents already created in DB before defining...
Read more >
Unique partial index is not working with mongoose on email
I am using mongodb version 4.4.8 and mongoose version 5.5.9. I have an user schema and this is how i define it
Read more >
Behavior of unique constraint while index is being built
I have a User collection with a uid field that needs to be unique, for which a unique constraint has been added. For...
Read more >
Unique Indexes — MongoDB Manual
MongoDB cannot create a unique index on the specified index field(s ) if the collection already contains data that would violate the unique...
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