"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")
}


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


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.
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
TThorge5/15/2023
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)
TThorge5/15/2023
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
Bbrody5/15/2023
if you can access it DATABASE_URL within python, then the issue is with prisma
TThorge5/15/2023
Alright, thank you!
Rroot5/15/2023
Aha, I found the fix!
Rroot5/15/2023
You need to add a variable reference in the Variables tab of your service.
Bbrody5/15/2023
nah
Rroot5/15/2023
huh?
Rroot5/15/2023
fixed it for me
Bbrody5/15/2023
they are using an external database from supabase
Rroot5/15/2023
oh
Rroot5/15/2023
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')
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.
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