"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:
When I run prisma db push I get the following error:
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
Project ID:
21c22b76-3eeb-4e49-8c44-af1f48a72060
show me a screenshot of your service variables?
I am not able to access any variables inside schema.prisma. I also tried to regenerate the client but that doesn't help.
where is database hosted
Supabase
isn't primsma a nodejs thing, but you mentioned python?
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"]
are you overriding that variable somewhere? like a .env file with a blank DATABASE_URL variable?
Nope, I don't have a single env file
this would be a prisma issue, not a railway issue, so I would suggest maybe searching for this issue on prisma forms / stack overflow.
it works if I create an .env file though? How is Railway setting the env variables?
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
Flagging this thread. A team member will be with you shortly.
Alright, thank you!
Aha, I found the fix!
You need to add a variable reference in the Variables tab of your service.
nah
huh?
fixed it for me
they are using an external database from supabase
oh
nvm
dont give me hope haha
wanna add a phase that will echo the DATABASE_URL variable while building?
For debugging that the variable is set correctly? will do
see if the variable is even available during build
Yes, the variable is printing correctly
I managed to get it to work with this hacky workaround that gets called on server start
the original error you got, was that error during build
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.https://www.prisma.io/docs/guides/development-environment/environment-variables/managing-env-files-and-setting-variables#using-the-system-environment-directly
I’d suggest reading the docs. You shouldn’t need to make a .env file.
Prisma
Managing .env files and setting variables
Learn how to manage .env files and set environment variables
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...I'm glad you have it solved