R
Railway

✋|help

"Environment variable not found"

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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Process killed even after upgrading to Developer planI was getting a 'killed' message when trying to run a process that takes some memory. I upgraded toHello, testingPlease ignore[Laravel] API Post & Delete methods don't worka7c0ce23-6ecb-418f-a849-b0b7613e09a0 I've built a simple API on Laravel and deployed it on Railway. Superuser privilege on Postgres for DMSHi, trying to perform a 0-downtime migration and I want to use the Railway database as the source foIs it possible to host a Python script which runs 24/7 on Railway?Title self explanatory.Limit the resources of a Postgres service (or any service)183ad877-28ff-4c3c-b049-e91b9ebbcfb7VPC Peering, pgvector, pgbouncer timelines if at allHey, trying to move off to a hosted database. We want to use AWS ideally; however, we're seeing quitAfter deploying the images are not showing and some pages have broken css.What could be the problemGet IP of incoming HTTP requestIs there a way to get incoming HTTP requests? Attempting to get the IP results in the IP of the railLatency and VPC peeringHey, trying to move off to a hosted database. We want to use AWS ideally; however, we're seeing quitProblem in calls url in my fronn end.I have a problem when i try to send method post on login in my applicatiion. In console the webbrowsFlask WebSockets Invalid session...I modified my Python Flask server to use socket.io-client and am seeing errors logged after deploy. Is there a way to attach block storage to a railway app?I am working on a Golang app with a Postgresql db and want to be able to run git commands (mainly clI want to deploy a nextjs website for a clientI wan to to deploy a nextjs website for a client. I'm thinking what is the best way to do this. Thishow do i tell railway to use the --force command on npm#11 1.881 npm ERR! Fix the upstream dependency conflict, or retry #11 1.882 npm ERR! this command wiDoes the TOS allow scraping websites?> “crawls,” “scrapes,” or “spiders” any page, data, or portion of or relating to the Services or ConwriteFile not saveAfter i start a new deployment the files i added with writeFile do not save and get deleted, is therI have a dockerfile, Can someone help me deploy it?If this isn't the right place lmk, I have never used docker and want to deploy this service.Free usage plan resources exceeded but I didn'thow to use and configure python websockets module and stuff.