Skip to content

Database connection error when using Prisma with Vite and SQL Server (PrismaClientInitializationError) #9971

Open
@JAllanMora

Description

@JAllanMora

Reproduction

1.-I migrated my project to Vite.

2.-Configuré Prisma para usar un datasource con SQL Server.

generator client {
    provider = "prisma-client-js"
}

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

3.- My .env file includes the following connection URL:

DATABASE_URL="sqlserver://<host>:<port>;database=<dbname>;user=<user>;password=<password>;encrypt=true;trustServerCertificate=true" 

4.- When trying to initialize the Remix project, which initializes the Prisma client, I get the following error:

PrismaClientInitializationError: Error creating a database connection. (Authentication failed for user sa) at r (C:\Users\Allan\Documentos\Wincaja-Apps\node_modules.pnpm@[email protected][email protected]\node_modules@prisma\client\runtime\library.js:108:2574)

5.-If I remove the special characters from the password in the .env file, the connection works fine. Additionally, if I include the connection string directly in the Prisma file, the connection also functions correctly, even if the password contains special characters.

System Info

System Info:

Output of prisma -v:
Prisma version 4.16.2

vite version:
vite 5.4.3

Operating System:
Windows 11

Used Package Manager

pnpm

Expected Behavior

Prisma should correctly handle connection credentials, even if the password contains special characters, when using environment variables in a project with Vite.

Actual Behavior

I get a PrismaClientInitializationError due to authentication failure when using a password with special characters in the environment variable (DATABASE_URL). However, if I hardcode the connection string with the same password directly in the Prisma file, the connection works fine. Despite the initialization error, commands like npx prisma db pull and npx prisma studio run successfully, indicating that the issue only occurs during project initialization when Prisma attempts to use the environment variable for the connection.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions