prisma: MongoDB "Cannot create namespace" on Mongo 4.2
Bug description
Since I could not get the Prisma MongoDB tutorial working with the DB provided, I used an existing Atlas instance which has Replica Sets configured. I even created a collection in case Prisma was unable to create a collection (blog). However, on first run through of index.ts on the “create post” instance, Prisma failed with the following error:
PrismaClientUnknownRequestError2 [PrismaClientUnknownRequestError]:
Invalid `prisma.post.create()` invocation in
/Users/ali/Documents/Work/ViaVid/Prisma/blog/index.ts:9:21
6 await prisma.$connect()
7
8 // Create the first post
→ 9 await prisma.post.create(
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawError { code: "263", message: "Cannot create namespace dev101.Post in multi-document transaction." } })
at cb (/Users/ali/Documents/Work/ViaVid/Prisma/blog/node_modules/@prisma/client/runtime/index.js:34804:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
clientVersion: '2.27.0'
}
How to reproduce
- Change MongoDB to using a DB with a replica set
- Run everything as per the example
- npx ts-node index.ts
Expected behavior
Expected posts to write to DB and to have them listed.
Prisma information
// We want to connect to the MongoDB datasource
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
// We want to generate a typescript client
// Since mongodb is a preview feature, we'll enable that.
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongodb"]
}
// Create our post schema. All posts have a title, slug and body.
// The id attributes tell Prisma it's a primary key and to generate
// object ids by default when we insert posts.
model Post {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
slug String @unique
title String
body String
}
Environment & setup
- OS: MacOSX 11.4
- Database: MongoDB 4.2.15, DB dev101 collection (pre-created manually) blog
- Node.js version: 14.17.3
Prisma Version
2.27.0
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 19 (12 by maintainers)
Re-opening since 4.2 is a version we’d like to support.
@matthewmueller reminded me that we actually have the perfect workaround for this
db push
:So running
prisma db push
creates the collection (and the index) so that when you run the script, the collection exists and data can be inserted.the 4.2 or lower issue really should only happen when you start with a new database w/o the collections created. Prisma tries to create the collections but can’t because 4.2 and lower don’t support creating collections in a transaction and it seems most or all of prismas calls are in a transaction scope. if you create your collections w/ migrate beforehand this doesnt seem to be an issue.
Yeah, we use transactions everywhere in the connector. I guess we can close this as Mongo 4.4+ is required for use with Prisma.
Yes. That situation should reproduce the issue.
I don’t have the exact query on hand anymore but it was pretty much what @janpio linked.