prisma: Invalid MongoDB connection string in database URL.
Bug description
Prisma does not want to connect to a Mongo Atlas cluster within Blitz.js.
I get the following error when I mutate ->
Error: Invalid `prisma.user.create()` invocation: The provided database string is invalid. Invalid MongoDB connection string in database URL. Please refer to the documentation in https://www.prisma.io/docs/reference/database-reference/connection-urls for constructing a correct connection string. In some cases, certain characters must be escaped. Please check the string for any illegal characters.
How to reproduce
- Set .ENV DATABASE_URL=“mongodb+srv://<username>:<password>@blitzdb.7x*3r.mongodb.net/myFirstDatabase?retryWrites=true&w=majority”
- Run
blitz prisma generate
- Run
blitz dev
- Try and signup a user
- Get error
Expected behavior
The user should be added to the database.
Prisma information
// 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 Int @id @default(autoincrement()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("USER")
tokens Token[]
sessions Session[]
}
model Session {
id Int @id @default(autoincrement()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
user User? @relation(fields: [userId], references: [id])
userId Int?
}
model Token {
id Int @id @default(autoincrement()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
// See note below about TokenType enum
// type TokenType
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}
// NOTE: It's highly recommended to use an enum for the token type
// but enums only work in Postgres.
// See: https://blitzjs.com/docs/database-overview#switch-to-postgresql
// enum TokenType {
// RESET_PASSWORD
// }
model Project {
id Int @id @default(autoincrement()) @map("_id")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
}
Environment & setup
- OS: macOS Big Sur 11.5.2 (20G95)
- Database: mongoDB 5.0.3
- Node.js version: v14.15.5
- Blitz.js 0.42.0
Prisma Version
Environment variables loaded from .env
prisma : 3.4.0
@prisma/client : 3.4.0
Current platform : darwin
Query Engine (Node-API) : libquery-engine 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/prisma/node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
Studio : 0.438.0
Preview Features : mongoDb
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 27 (6 by maintainers)
Perhaps this answer can help you a little, When you create database from mongo atlas, and try to connect with application using node js. Atlas will give database url as follows
if you copy and paste in DATABASE_URL, Then the Invalid MongoDB connection string error will appear.
The problem is special characters question mark “?” in the connection string, you can change “?” to “%3F” or change it to
DATABASE_URL=“mongodb+srv://<database>:<password>@cluster0.zf8ptns.mongodb.net/cluster0retryWrites=true&w=majority”
While the suggested solution here appears to work, I think the real issue is when you copy a Mongo Atlas connection string, there will not be a database name provided by default.
Compare:
mongodb+srv://youruser:<password>@yoururl.mongodb.net/?retryWrites=true&w=majority
with…
mongodb+srv://youruser:<password>@yoururl.mongodb.net/mydatabasename?retryWrites=true&w=majority
TLDR
Make sure to specify a database name (
mydatabasename
):mongodb+srv://youruser:<password>@yoururl.mongodb.net/mydatabasename?retryWrites=true&w=majority
Put /test in the end of url. It will work
thanks the solution worked well!
@janpio It’s really a problem caused by special characters。i create a new username no special characters。It’s work。 why prisma can’t parse characters?
Maybe this can help https://youtu.be/ag1TfainlLo
Any special characters in the url needs to be percent encoded. https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding
I think this issue is already solved so I am going to close this.
Because no on ever reported this as a problem before and we seem to have missed it in our own testing. If this is indeed the underlying reason for this error, I am confident we can fix it.
@regardt-nel @lewis45454 @mgcrea Did your connection strings also include non a-Z0-9 characters?