"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
Percy2y ago
Project ID: 21c22b76-3eeb-4e49-8c44-af1f48a72060
Brody
Brody2y ago
show me a screenshot of your service variables?
Thorge
ThorgeOP2y ago
Thorge
ThorgeOP2y 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
Brody2y ago
where is database hosted
Thorge
ThorgeOP2y ago
Supabase
Brody
Brody2y ago
isn't primsma a nodejs thing, but you mentioned python?
Thorge
ThorgeOP2y 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
Brody2y ago
are you overriding that variable somewhere? like a .env file with a blank DATABASE_URL variable?
Thorge
ThorgeOP2y ago
Nope, I don't have a single env file
Brody
Brody2y 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
ThorgeOP2y ago
it works if I create an .env file though? How is Railway setting the env variables?
Brody
Brody2y 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
Percy2y ago
Flagging this thread. A team member will be with you shortly.
Thorge
ThorgeOP2y ago
Alright, thank you!
root
root2y ago
Aha, I found the fix! You need to add a variable reference in the Variables tab of your service.
Brody
Brody2y ago
nah
root
root2y ago
huh? fixed it for me
Brody
Brody2y ago
they are using an external database from supabase
root
root2y ago
oh nvm
Thorge
ThorgeOP2y ago
dont give me hope haha
Brody
Brody2y ago
wanna add a phase that will echo the DATABASE_URL variable while building?
Thorge
ThorgeOP2y ago
For debugging that the variable is set correctly? will do
Brody
Brody2y ago
see if the variable is even available during build
Thorge
ThorgeOP2y 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
Brody2y ago
the original error you got, was that error during build
Thorge
ThorgeOP2y 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
luna2y ago
Prisma
Managing .env files and setting variables
Learn how to manage .env files and set environment variables
Thorge
ThorgeOP2y 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
Brody2y ago
I'm glad you have it solved
Want results from more Discord servers?
Add your server