Prisma not connecting to DB in amplify

Hey guys so i got the following schema
model Images {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
model Model @relation(fields: [modelId], references: [id])
modelId Int
userId String
image String
}
model Images {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
model Model @relation(fields: [modelId], references: [id])
modelId Int
userId String
image String
}
and in one of my api endpoints i am doing
await prisma.images.create({
data: {
user: {
connect: {
id: userId,
},
},
model: {
connect: {
id: modelId,
},
},
image: fileName,
},
});
await prisma.images.create({
data: {
user: {
connect: {
id: userId,
},
},
model: {
connect: {
id: modelId,
},
},
image: fileName,
},
});
this works perfectly locally and nothing goes wrong but then when i pushed it to prod i kept getting Internal Server Error I added a try catch block to see what the error is and it gave me {"error":{"clientVersion":"4.7.1"}} I dont know what im doing wrong. any ideas?
98 Replies
bakdaddy
bakdaddyβ€’12mo ago
well, from the looks of the query, it either doesn't find a corresponding user or a corresponding model entries by the id you provide
elpupper
elpupperβ€’12mo ago
im using the same exact post request just changed the url it works locally (the db is connected to supabase) but i see what you mean not sure how it cant find a corresponding user nor model
bakdaddy
bakdaddyβ€’12mo ago
could you share your error handling code? I'd also check your env variables for connections to make sure it all is connected properly https://supabase.com/docs/guides/integrations/prisma#step-2-testing-the-connection also i'd look in https://www.prisma.io/docs/guides/database/supabase
elpupper
elpupperβ€’12mo ago
it is just a try and catch block ill have a look at this i do have a feeling its not connecting
bakdaddy
bakdaddyβ€’12mo ago
yeah, probably
elpupper
elpupperβ€’12mo ago
thanks gonna have a look i na second wait so i cant have more than 1 connection to the db? so i have to use pgbouncer ?
bakdaddy
bakdaddyβ€’12mo ago
Could be, there's a section on it in the supabase's docs
elpupper
elpupperβ€’12mo ago
yep it looks like it
bakdaddy
bakdaddyβ€’12mo ago
are you doing serverless?
elpupper
elpupperβ€’12mo ago
yes
bakdaddy
bakdaddyβ€’12mo ago
yeah, in that case you'll need either somehow close each connection (but it's not really optimal at all with prisma) after calling the functions, or use some sort of proxy pooling - https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/connection-pool https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#serverless-environments-faas this looks promising https://www.prisma.io/data-platform/proxy it has a free tier
elpupper
elpupperβ€’12mo ago
ok i see i need to update my prisma to a version higher than 4.10 for the directUrl Feature wonder if that would break anything honestly might just use planetscale well im using planetscale and im still getting the same issue so i think its just simply not connecting
bakdaddy
bakdaddyβ€’12mo ago
Just update it, it shouldn't break anything
elpupper
elpupperβ€’12mo ago
just did still not working prisma isnt connecting to the db now i have no clue why i renamed the question its building the build process goes through no errors at all then when it comes to mutating just errors bump
elpupper
elpupperβ€’12mo ago
elpupper
elpupperβ€’11mo ago
?? how im boughta just hardcode that value fuck env vars in AWS bump
elpupper
elpupperβ€’11mo ago
elpupper
elpupperβ€’11mo ago
i already have it there
elpupper
elpupperβ€’11mo ago
bakdaddy
bakdaddyβ€’11mo ago
retry npx prisma generate or double check for the variable and syntax (maybe try quotes or remove quotes if they are there)
elpupper
elpupperβ€’11mo ago
so after i build i try npx prisma generate or before build
bakdaddy
bakdaddyβ€’11mo ago
I do before build
elpupper
elpupperβ€’11mo ago
cause from my understanding it does it in the build
bakdaddy
bakdaddyβ€’11mo ago
in my scripts in package.json i have "build": "prisma generate && next build", sorry if not much help, as I never yet used Amplify
elpupper
elpupperβ€’11mo ago
very much understandable thank you for atleast trying
bakdaddy
bakdaddyβ€’11mo ago
aws is too confusing for me lol
elpupper
elpupperβ€’11mo ago
😭 vercel uses AWS idk what they did to make this black magic work
bakdaddy
bakdaddyβ€’11mo ago
yeah, and they very nicely abstract all of the dumb stuff
elpupper
elpupperβ€’11mo ago
more like bs stuff
bakdaddy
bakdaddyβ€’11mo ago
i suppose having a lot of staff helps lol maybe try adding a .env file ? or you could do something like this in the build script for amplify
build:
commands:
- npm run build:$BUILD_ENV
- echo "TWITCH_CLIENT_ID=$TWITCH_CLIENT_ID" >> backend/.env
build:
commands:
- npm run build:$BUILD_ENV
- echo "TWITCH_CLIENT_ID=$TWITCH_CLIENT_ID" >> backend/.env
even maybe
build:
commands:
- echo "TWITCH_CLIENT_ID=$TWITCH_CLIENT_ID" >> backend/.env
- npm run build:$BUILD_ENV
build:
commands:
- echo "TWITCH_CLIENT_ID=$TWITCH_CLIENT_ID" >> backend/.env
- npm run build:$BUILD_ENV
but obv replace values for prisma https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html#access-env-vars
elpupper
elpupperβ€’11mo ago
ive done those as well ok so when i did npx db-generate i got env errors and i fixed those now i wasnt getting them when building but now its still not FUCKING recognizing the env var in runtime
bakdaddy
bakdaddyβ€’11mo ago
dang it, roll back bro
elpupper
elpupperβ€’11mo ago
to where
bakdaddy
bakdaddyβ€’11mo ago
to when it didn't give errors with db-generate
elpupper
elpupperβ€’11mo ago
There is no errors anymore I fixed it Let it be known im still getting env var not defined in runtime But not build
bakdaddy
bakdaddyβ€’11mo ago
try removing the query and console.log the env var somewhere?
elpupper
elpupperβ€’11mo ago
what query ill try and log it ok i just console log the var and its fucking there ITS THERE but in prisma ITS NOT >?????
bakdaddy
bakdaddyβ€’11mo ago
so there you go, it's prisma's fault, try adding to the node build script 'prisma generate', maybe somehow the client (prisma client) doesn't see the var
elpupper
elpupperβ€’11mo ago
so i did some digging prisma is trying to look for a .env file but there is no .env file even tho i specified it
bakdaddy
bakdaddyβ€’11mo ago
you could possibly just make one?
elpupper
elpupperβ€’11mo ago
can i?
bakdaddy
bakdaddyβ€’11mo ago
or it's ignored by git or something
elpupper
elpupperβ€’11mo ago
ofc its ignored
bakdaddy
bakdaddyβ€’11mo ago
how do you get your files to amplify?
elpupper
elpupperβ€’11mo ago
git
bakdaddy
bakdaddyβ€’11mo ago
from git? then it doesn't pull it
elpupper
elpupperβ€’11mo ago
yeah git clones then it uses the env vars i specificed in AWS
bakdaddy
bakdaddyβ€’11mo ago
you need to make a '.env' file and set it's contents from your AWS env vars as part of your building script like the 'toml' script or whatnot
elpupper
elpupperβ€’11mo ago
thats what - echo "DATABASE_URL=$DATABASE_URL" >> .env is for no?
bakdaddy
bakdaddyβ€’11mo ago
or yaml yeah
elpupper
elpupperβ€’11mo ago
yeah im doing that
bakdaddy
bakdaddyβ€’11mo ago
try with one > echo "DATABASE_URL=$DATABASE_URL" > .env idk should do it maybe put it befor npm run build
elpupper
elpupperβ€’11mo ago
im doing it all at once πŸ˜‚
bakdaddy
bakdaddyβ€’11mo ago
what's your build step in the script?
elpupper
elpupperβ€’11mo ago
what does the arrow mean?
bakdaddy
bakdaddyβ€’11mo ago
put 'echo's return into the file
elpupper
elpupperβ€’11mo ago
version: 1
applications:
- frontend:
phases:
preBuild:
commands:
- npm install -g pnpm@8.5.1
- pnpm --version
build:
commands:
- pnpm --filter \!expo install --store=node_modules/.pnpm-store
- echo "DATABASE_URL=$DATABASE_URL" >> .env
- pnpm db-generate
- pnpm db-push
- npx turbo run build --filter=nextjs
artifacts:
baseDirectory: apps/nextjs/.next
files:
- '**/*'
cache:
paths:
- node_modules/**/*
buildPath: /
appRoot: apps/nextjs
version: 1
applications:
- frontend:
phases:
preBuild:
commands:
- npm install -g pnpm@8.5.1
- pnpm --version
build:
commands:
- pnpm --filter \!expo install --store=node_modules/.pnpm-store
- echo "DATABASE_URL=$DATABASE_URL" >> .env
- pnpm db-generate
- pnpm db-push
- npx turbo run build --filter=nextjs
artifacts:
baseDirectory: apps/nextjs/.next
files:
- '**/*'
cache:
paths:
- node_modules/**/*
buildPath: /
appRoot: apps/nextjs
bakdaddy
bakdaddyβ€’11mo ago
so put "DATABASE_URL=$DATABASE_URL" in the file and what does your db-generate look like in your package.json?
elpupper
elpupperβ€’11mo ago
"db-push": "pnpm with-env prisma db push",
"db-generate": "pnpm with-env prisma generate"
"db-push": "pnpm with-env prisma db push",
"db-generate": "pnpm with-env prisma generate"
it generates just fine pushes just fine
bakdaddy
bakdaddyβ€’11mo ago
ok I think i got something, I think it's not sending it to the proper place? can you look through the files in prod? like find where it generated this .env file?
elpupper
elpupperβ€’11mo ago
im not sure i f i can do that thats what im tryna figure out
bakdaddy
bakdaddyβ€’11mo ago
cause it says baseDirectory: apps/nextjs/.next
elpupper
elpupperβ€’11mo ago
the db wouldnt generate nore push if the .env doesnt exist so it is going through but then after the build it doesnt
bakdaddy
bakdaddyβ€’11mo ago
not sure, cause it has this with-env arg
elpupper
elpupperβ€’11mo ago
let me see rq
"with-env": "dotenv -e ../../.env --",
"with-env": "dotenv -e ../../.env --",
bakdaddy
bakdaddyβ€’11mo ago
try echo "DATABASE_URL=$DATABASE_URL" >> ../.env i guess it's not getting the correct .env 😦 for prisma client
elpupper
elpupperβ€’11mo ago
yeah :/
bakdaddy
bakdaddyβ€’11mo ago
Or try echo ... >> ../../.env
elpupper
elpupperβ€’11mo ago
thats what i just had ill try it again should i do it before build or after
bakdaddy
bakdaddyβ€’11mo ago
Before I think
elpupper
elpupperβ€’11mo ago
ok nope my sanity im losing it im losing it
bakdaddy
bakdaddyβ€’11mo ago
Go take a breezer man
elpupper
elpupperβ€’11mo ago
no i need to fix this
bakdaddy
bakdaddyβ€’11mo ago
It's important to stay theoScaredZoom
elpupper
elpupperβ€’11mo ago
i was meant to deploy this last week
bakdaddy
bakdaddyβ€’11mo ago
Do you have logs of building process? Could you find the place where it generates prisma client? You could also try to eche into .env file inside prisma folder, where you have schema before build time
elpupper
elpupperβ€’11mo ago
oh yeahh i can try and echo the .env into the prisma couldnt i
bakdaddy
bakdaddyβ€’11mo ago
Yeah it's a possibility But need to find prisma Looks like the build path is actually "/" so no need for ../.. before .env
elpupper
elpupperβ€’11mo ago
how so
bakdaddy
bakdaddyβ€’11mo ago
In your yaml file Beforelast line It's just some thoughts
elpupper
elpupperβ€’11mo ago
where what
bakdaddy
bakdaddyβ€’11mo ago
bakdaddy
bakdaddyβ€’11mo ago
Maybe it's not too late to try https://sst.dev/chapters/configure-aws-amplify.html ???
SST
Configure AWS Amplify
We are going to use the information of our AWS resources to configure AWS Amplify in our React app. We'll call the Amplify.configure() method when our app first loads.
elpupper
elpupperβ€’11mo ago
maybe i will admit defeat now but one day ill comeback to amplify and try and do it without sst
bakdaddy
bakdaddyβ€’11mo ago
Bezos should burn lol
elpupper
elpupperβ€’11mo ago
agreed
bakdaddy
bakdaddyβ€’11mo ago
Sorry bro, as I said, I don't much use Amplify (not at all actually) and couldn't help out much
elpupper
elpupperβ€’11mo ago
totally valid i dont blame u
bakdaddy
bakdaddyβ€’11mo ago
We tried though If you do manage to fix it, tell me, cause I'm intrigued lol Or if you have more questions, I'll try to help as well
elpupper
elpupperβ€’11mo ago
πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ forsure man i will try my very best might make a issue in the prisma repo guess what i just found
elpupper
elpupperβ€’11mo ago
GitHub
turbo/examples/with-prisma at main Β· vercel/turbo
Incremental bundler and build system optimized for JavaScriptΒ and TypeScript, written in Rust – including Turbopack and Turborepo. - turbo/examples/with-prisma at main Β· vercel/turbo
bakdaddy
bakdaddyβ€’11mo ago
Is it much different from yours?
elpupper
elpupperβ€’11mo ago
no thats the same file structure im pushing pray with me please πŸ™ it didnt work man dfhskljsdhgfijnbsdgf njkosdfgh sdfhglkmn;sdfhgl;mkhdfg
bakdaddy
bakdaddyβ€’11mo ago
Sorry chief
elpupper
elpupperβ€’11mo ago
no need to say sorry its not ur fault i opened a github issue at prisma hopefully someone answers https://github.com/prisma/prisma/discussions/20116 OKAYY I FIXED IT LETS GOOO
elpupper
elpupperβ€’11mo ago
GitHub
DATABASE_URL not found in Amplify Β· prisma prisma Β· Discussion #201...
I have the current Turborepo setup https://github.com/t3-oss/create-t3-turbo/tree/main I am trying to deploy to AWS Amplify but i am having issues with env vars specifically the DATABASE_URL. I fol...
bakdaddy
bakdaddyβ€’11mo ago
Let's go! Good job, chief