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β€’2y 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
elpupperOPβ€’2y 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β€’2y 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
elpupperOPβ€’2y 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β€’2y ago
yeah, probably
elpupper
elpupperOPβ€’2y 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β€’2y ago
Could be, there's a section on it in the supabase's docs
elpupper
elpupperOPβ€’2y ago
yep it looks like it
bakdaddy
bakdaddyβ€’2y ago
are you doing serverless?
elpupper
elpupperOPβ€’2y ago
yes
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y 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β€’2y ago
Just update it, it shouldn't break anything
elpupper
elpupperOPβ€’2y 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
elpupperOPβ€’2y ago
elpupper
elpupperOPβ€’2y ago
?? how im boughta just hardcode that value fuck env vars in AWS bump
elpupper
elpupperOPβ€’2y ago
elpupper
elpupperOPβ€’2y ago
i already have it there
elpupper
elpupperOPβ€’2y ago
bakdaddy
bakdaddyβ€’2y ago
retry npx prisma generate or double check for the variable and syntax (maybe try quotes or remove quotes if they are there)
elpupper
elpupperOPβ€’2y ago
so after i build i try npx prisma generate or before build
bakdaddy
bakdaddyβ€’2y ago
I do before build
elpupper
elpupperOPβ€’2y ago
cause from my understanding it does it in the build
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
very much understandable thank you for atleast trying
bakdaddy
bakdaddyβ€’2y ago
aws is too confusing for me lol
elpupper
elpupperOPβ€’2y ago
😭 vercel uses AWS idk what they did to make this black magic work
bakdaddy
bakdaddyβ€’2y ago
yeah, and they very nicely abstract all of the dumb stuff
elpupper
elpupperOPβ€’2y ago
more like bs stuff
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y 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β€’2y ago
dang it, roll back bro
elpupper
elpupperOPβ€’2y ago
to where
bakdaddy
bakdaddyβ€’2y ago
to when it didn't give errors with db-generate
elpupper
elpupperOPβ€’2y 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β€’2y ago
try removing the query and console.log the env var somewhere?
elpupper
elpupperOPβ€’2y 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β€’2y 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
elpupperOPβ€’2y 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β€’2y ago
you could possibly just make one?
elpupper
elpupperOPβ€’2y ago
can i?
bakdaddy
bakdaddyβ€’2y ago
or it's ignored by git or something
elpupper
elpupperOPβ€’2y ago
ofc its ignored
bakdaddy
bakdaddyβ€’2y ago
how do you get your files to amplify?
elpupper
elpupperOPβ€’2y ago
git
bakdaddy
bakdaddyβ€’2y ago
from git? then it doesn't pull it
elpupper
elpupperOPβ€’2y ago
yeah git clones then it uses the env vars i specificed in AWS
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
thats what - echo "DATABASE_URL=$DATABASE_URL" >> .env is for no?
bakdaddy
bakdaddyβ€’2y ago
or yaml yeah
elpupper
elpupperOPβ€’2y ago
yeah im doing that
bakdaddy
bakdaddyβ€’2y ago
try with one > echo "DATABASE_URL=$DATABASE_URL" > .env idk should do it maybe put it befor npm run build
elpupper
elpupperOPβ€’2y ago
im doing it all at once πŸ˜‚
bakdaddy
bakdaddyβ€’2y ago
what's your build step in the script?
elpupper
elpupperOPβ€’2y ago
what does the arrow mean?
bakdaddy
bakdaddyβ€’2y ago
put 'echo's return into the file
elpupper
elpupperOPβ€’2y ago
version: 1
applications:
- frontend:
phases:
preBuild:
commands:
- npm install -g [email protected]
- 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 [email protected]
- 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β€’2y ago
so put "DATABASE_URL=$DATABASE_URL" in the file and what does your db-generate look like in your package.json?
elpupper
elpupperOPβ€’2y 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β€’2y 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
elpupperOPβ€’2y ago
im not sure i f i can do that thats what im tryna figure out
bakdaddy
bakdaddyβ€’2y ago
cause it says baseDirectory: apps/nextjs/.next
elpupper
elpupperOPβ€’2y 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β€’2y ago
not sure, cause it has this with-env arg
elpupper
elpupperOPβ€’2y ago
let me see rq
"with-env": "dotenv -e ../../.env --",
"with-env": "dotenv -e ../../.env --",
bakdaddy
bakdaddyβ€’2y ago
try echo "DATABASE_URL=$DATABASE_URL" >> ../.env i guess it's not getting the correct .env 😦 for prisma client
elpupper
elpupperOPβ€’2y ago
yeah :/
bakdaddy
bakdaddyβ€’2y ago
Or try echo ... >> ../../.env
elpupper
elpupperOPβ€’2y ago
thats what i just had ill try it again should i do it before build or after
bakdaddy
bakdaddyβ€’2y ago
Before I think
elpupper
elpupperOPβ€’2y ago
ok nope my sanity im losing it im losing it
bakdaddy
bakdaddyβ€’2y ago
Go take a breezer man
elpupper
elpupperOPβ€’2y ago
no i need to fix this
bakdaddy
bakdaddyβ€’2y ago
It's important to stay theoScaredZoom
elpupper
elpupperOPβ€’2y ago
i was meant to deploy this last week
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
oh yeahh i can try and echo the .env into the prisma couldnt i
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
how so
bakdaddy
bakdaddyβ€’2y ago
In your yaml file Beforelast line It's just some thoughts
elpupper
elpupperOPβ€’2y ago
where what
bakdaddy
bakdaddyβ€’2y ago
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
maybe i will admit defeat now but one day ill comeback to amplify and try and do it without sst
bakdaddy
bakdaddyβ€’2y ago
Bezos should burn lol
elpupper
elpupperOPβ€’2y ago
agreed
bakdaddy
bakdaddyβ€’2y ago
Sorry bro, as I said, I don't much use Amplify (not at all actually) and couldn't help out much
elpupper
elpupperOPβ€’2y ago
totally valid i dont blame u
bakdaddy
bakdaddyβ€’2y 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
elpupperOPβ€’2y ago
πŸ˜‚ πŸ˜‚ πŸ˜‚ πŸ˜‚ forsure man i will try my very best might make a issue in the prisma repo guess what i just found
elpupper
elpupperOPβ€’2y 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β€’2y ago
Is it much different from yours?
elpupper
elpupperOPβ€’2y ago
no thats the same file structure im pushing pray with me please πŸ™ it didnt work man dfhskljsdhgfijnbsdgf njkosdfgh sdfhglkmn;sdfhgl;mkhdfg
bakdaddy
bakdaddyβ€’2y ago
Sorry chief
elpupper
elpupperOPβ€’2y 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
elpupperOPβ€’2y 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β€’2y ago
Let's go! Good job, chief
Want results from more Discord servers?
Add your server