Prisma with MongoDB fails when you create a User
prisma.user.create() fails against MongoDB in a Next.js route or server actionShort answer
Prisma's MongoDB connector needs a replica set, because it uses transactions for nested writes. It also needs the id field mapped to _id as an ObjectId, and it has no Migrate support, so you apply the schema with prisma db push. Check those three things in order.
Fix it fast
1Find out which cause you have
Look at the model and the datasource first. Both should match the MongoDB form shown in the Prisma docs.
sed -n 1,40p prisma/schema.prisma2Match what you see, then jump to its fix
- 1
A single local mongod works for reads, and writes that Prisma wraps in a transaction fail.The MongoDB server is not a replica setFix - 2
The model uses id Int or id String @id without a MongoDB mapping.The id field is not mapped to _id as an ObjectIdFix - 3
The schema was never pushed, so the unique index on email does not exist.prisma migrate is used, but MongoDB has no MigrateFix
- 1
What does the error look like?
prisma.user.create() fails against MongoDB in a Next.js route or server actionThe User model does not work with the mongodb provider
Cause 1 of 3
The MongoDB server is not a replica set
1Check
The Prisma docs say transactions require a replica set deployment, and that Prisma uses MongoDB transactions internally to avoid partial writes on nested queries. A standalone mongod is not one.
mongosh "mongodb://localhost:27017" --eval "rs.status()"2Fix
Set replication.replSetName in the mongod config file, restart mongod, then run rs.initiate() once. The MongoDB guide says you only initiate the set once. Keep bindIp on localhost for a local set.
# mongod.conf
replication:
replSetName: "rs0"
net:
bindIp: localhost
# in mongosh, once
rs.initiate()3Verify
rs.status() now lists a member, and prisma.user.create() succeeds.
mongosh "mongodb://localhost:27017" --eval "rs.status()"Confirmed by mongodb.com
Cause 2 of 3
The id field is not mapped to _id as an ObjectId
1Check
MongoDB documents have an _id field. The Prisma docs require mapping it with @map("_id"), and an ObjectId field needs @db.ObjectId. @default(auto()) generates one.
grep -n -B2 -A8 "model User" prisma/schema.prisma2Fix
Declare the id the MongoDB way and set the provider to mongodb.
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
}3Verify
Regenerate the client, then create a user.
npx prisma generateConfirmed by prisma.io
Cause 3 of 3
prisma migrate is used, but MongoDB has no Migrate
1Check
The Prisma docs say there are no plans to add Prisma Migrate for MongoDB, and that @unique indexes are managed through db push.
grep -n "migrate" package.json2Fix
Apply the schema with db push, then generate the client yourself: the CLI docs say db push no longer runs generate from Prisma 7.
npx prisma db push
npx prisma generate3Verify
Create the same email twice: the second write should be rejected by the unique index.
npx prisma db pushConfirmed by prisma.io
How do I stop it coming back?
- Run a replica set in development as well, not a plain standalone mongod.
- Copy the id line from the Prisma MongoDB docs for every model.
- Run prisma db push in your setup script instead of Migrate commands.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
asyncpg InterfaceError: another operation is in progress with SQLAlchemy
InterfaceError raised from a FastAPI endpoint that runs several queries at once
A single database connection can run one operation at a time.
4 causes, each with a fixDatabases
A query on Aurora MySQL dies with 'table /rdsdbdata/tmp/#sql... is full'
ERROR 1114 (HY000): The table '/rdsdbdata/tmp/#sqlfd_1586_2' is full
In Aurora MySQL 3, internal temporary tables share one pool, and when that pool and the memory-mapped overflow file are used up, a reader instance ends the query with error 1114.
3 causes, each with a fixDatabases
Azure Database for MySQL refuses a connection that does not use SSL
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
Azure Database for MySQL Flexible Server requires encrypted connections by default (require_secure_transport = ON, TLS 1.2).
4 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
Dapper cannot find a constructor that fits the query columns
The error appears when Dapper maps rows into a record or a class whose only constructor takes arguments
Dapper looks for a public parameterless constructor first.
3 causes, each with a fix