R
Railway15mo ago
Thorge

"Environment variable not found"

Hi Railway Team! I have the URL of my postgres db stored inside an environment variable that I want to access in my prisma.schema:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
When I run prisma db push I get the following error:
error: Environment variable not found: DATABASE_URL.
--> schema.prisma:3
|
2 | provider = "postgresql"
3 | url = env("DATABASE_URL")
|

Validation Error Count: 1
error: Environment variable not found: DATABASE_URL.
--> schema.prisma:3
|
2 | provider = "postgresql"
3 | url = env("DATABASE_URL")
|

Validation Error Count: 1
I can read successfully the environment variable inside my main.py with: print("db_url", os.environ.get("DATABASE_URL")) When I create an .env file w/ the DATABASE_URL variable either in the prisma folder or in the root of the project, I can successfully run prismadb push This is super confusing to me. Any help would be appreciated.
30 Replies
Percy
Percy15mo ago
Project ID: 21c22b76-3eeb-4e49-8c44-af1f48a72060
Brody
Brody15mo ago
show me a screenshot of your service variables?
Thorge
Thorge15mo ago
Thorge
Thorge15mo ago
I am not able to access any variables inside schema.prisma. I also tried to regenerate the client but that doesn't help.
Brody
Brody15mo ago
where is database hosted
Thorge
Thorge15mo ago
Supabase
Brody
Brody15mo ago
isn't primsma a nodejs thing, but you mentioned python?
Thorge
Thorge15mo ago
There is a prisma python client The code works if I have the URL directly inside the prisma.schema or in an .env file inside the prisma folder or project root. The prisma.schema isn't able to find any environment variable that is supplied by Railway. I can access the environment variables inside main.py (starts my FastAPI server, is in the root of the project) This is my nixpacks.toml: nixpacks.toml Include auto-detected provider and Python provider providers = ["...", "python"] Install Node.js and other required packages during the setup phase [phases.setup] nixPkgs = ["...", "nodejs"] Add a new phase for installing the Prisma CLI [phases.install_prisma] cmds = ["npm install -g prisma"] dependsOn = ["install"] Add a new phase for Prisma generation [phases.prisma_generate] cmds = ["prisma generate"] dependsOn = ["install_prisma"] Update the build phase to depend on the new 'prisma_generate' phase [phases.build] dependsOn = ["...", "prisma_generate"]
Brody
Brody15mo ago
are you overriding that variable somewhere? like a .env file with a blank DATABASE_URL variable?
Thorge
Thorge15mo ago
Nope, I don't have a single env file
Brody
Brody15mo ago
this would be a prisma issue, not a railway issue, so I would suggest maybe searching for this issue on prisma forms / stack overflow.
Thorge
Thorge15mo ago
it works if I create an .env file though? How is Railway setting the env variables?
Brody
Brody15mo ago
they are passed in as environment variable to the docker image if you can access it DATABASE_URL within python, then the issue is with prisma
Percy
Percy15mo ago
Flagging this thread. A team member will be with you shortly.
Thorge
Thorge15mo ago
Alright, thank you!
root
root15mo ago
Aha, I found the fix! You need to add a variable reference in the Variables tab of your service.
Brody
Brody15mo ago
nah
root
root15mo ago
huh? fixed it for me
Brody
Brody15mo ago
they are using an external database from supabase
root
root15mo ago
oh nvm
Thorge
Thorge15mo ago
dont give me hope haha
Brody
Brody15mo ago
wanna add a phase that will echo the DATABASE_URL variable while building?
Thorge
Thorge15mo ago
For debugging that the variable is set correctly? will do
Brody
Brody15mo ago
see if the variable is even available during build
Thorge
Thorge15mo ago
Yes, the variable is printing correctly I managed to get it to work with this hacky workaround that gets called on server start
def set_db_env():
database_url = os.environ.get('DATABASE_URL')

with open('.env', 'w') as f:
f.write(f'DATABASE_URL={database_url}\n')
def set_db_env():
database_url = os.environ.get('DATABASE_URL')

with open('.env', 'w') as f:
f.write(f'DATABASE_URL={database_url}\n')
Brody
Brody15mo ago
the original error you got, was that error during build
Thorge
Thorge15mo ago
No that was locally. If I run echo DATABASE_URL: $DATABASE_URL I get returned an empty string: DATABASE_URL: Maybe there is just something wrong with my terminal setup? I thought I checked that the deployment also didn't work but I might have tested the wrong environment. Just started new deployment build w/o my workaround.
luna
luna15mo ago
Prisma
Managing .env files and setting variables
Learn how to manage .env files and set environment variables
Thorge
Thorge15mo ago
Alright everyone, I figured it out. Postmortem: 1. prisma.schema had access to the env variable during runtime when it accessed it the whole time, both locally and on the deployed server. I should have better tested this directly in the beginning. 2. The only thing that didn't work was the prisma db push terminal command. I found out that I should run railway run prisma db instead to have access to the env variables. Somehow that didn't have the DATABASE_URL variable (confirmed via railway run printenv). I deleted my railway config.json and relogged in and relinked and then I had access to the env variable inside railway run. Sorry for the hassle everyone! And thanks for being so attentive and helpful.❤️ Learned some new things...
Brody
Brody15mo ago
I'm glad you have it solved