P
Prisma2mo ago
ahcheng

Why doesn't the generated client use the .env file specified by dotenv?

package.json
{
"scripts": {
"migrate:dev": "dotenv -e .env.development -- npx prisma migrate dev"
}
}
{
"scripts": {
"migrate:dev": "dotenv -e .env.development -- npx prisma migrate dev"
}
}
.env
DATABASE_URL="mysql://xxx/t-prisma-test?schema=public"
DATABASE_URL="mysql://xxx/t-prisma-test?schema=public"
.env.development
DATABASE_URL="mysql://xxx/t-prisma-test-dev?schema=public"
DATABASE_URL="mysql://xxx/t-prisma-test-dev?schema=public"
After executing pnpm run migrate:dev, Prisma correctly loads the .env.development and creates tables in the t-prisma-test-dev database. However, when I query data using the client, I encounter the following error: 'Database t-prisma-test does not exist on the database server at localhost:3306. Why is the client operating on the t-prisma-test database specified in the .env file?'
1 Reply
RaphaelEtim
RaphaelEtim2mo ago
Hi @ahcheng 👋 The environment variables used by Prisma Client are loaded from the .env file by default. While your migration script correctly uses the .env.development file due to the dotenv command, the Prisma Client does not automatically do so. To resolve this issue, you need to ensure that the Prisma Client also uses the .env.development file when you are working in a development environment. One way to achieve this is by setting the DATABASE_URL environment variable explicitly before running your application. You can modify your package.json scripts to include this:
{
"scripts": {
"migrate:dev": "dotenv -e .env.development -- npx prisma migrate dev",
"start:dev": "dotenv -e .env.development -- node yourApp.js"
}
}
{
"scripts": {
"migrate:dev": "dotenv -e .env.development -- npx prisma migrate dev",
"start:dev": "dotenv -e .env.development -- node yourApp.js"
}
}