Mysql + Golang App
So I've deployed a mysql database, and then deployed a golang api from github... I added a reference to the MYSQL_URL in the variables for my golang app. But my app cant connect to it. Its able to connect to a local db just fine. But i get these logs that the default addr for the mysql app is unknown then my app crashes.
36 Replies
can you show a screenshot of your service variables
Tht DB_CONNECTION one isn't being used. I just added it to test
show me the code that connects to the database please
I have MYSQL_URL set to a local MySQL db connection string in a local .env file. And it connects to that just fine
everything looks fine, i will do some tests and get back to you
Okay thanks
MYSQL_URL
is a URL, but gorm expects a DSN, so you can use github.com/xo/dburl
to parse the DSN from the URLwell, that gets me farther! its not crashing, new error though
for some context, this is what the rest of my main.go looks like. After connecting to the db, I do an automigrate a bunch of gorm models (example in img 2). Then run a method for populating initial data in some of the tables (img 3). Then start a cron scheduler (that currently does nothing but print to the log), and then expose a graphql http route
can you show me where that error comes from @agi_marvin
its also in the deploy logs
i meant where in the code does that error get logged
it doesnt. you're looking at the entire project pretty much in those 3 images above. this is the full main.go
wdym it doesnt?? where is that error logged from??
its not getting logged from anything in my project
i dont know where its coming from. If its a gorm error, a railway error. searching it I get nothing
you are printing an err returned from a function call, what print is printing that error
from gorm.Open
the code in the production if block is not the same as mine
are you useing an outdated gorm package or something?
this block you included threw an error
^
im checking. i just started the project yesterday, so it shouldnt be
im using
gorm.io/gorm
yeah thats a fork of gorm from 2021
i know what the issue is now, but first update your code to use gorm.io/gorm
alright, I'm updated to gorm.io
tested it locally and its good there. Automigrate now failing on the railway db though
okay next issue
you are throwing out the
*gorm.DB
return value while on railwayalso, instead of setting your own
ENV
variable, just use the one automatically provided by railway
https://docs.railway.app/develop/variables#railway-provided-variablesah good catch. copy / paste failure on my part.
we are up and running! thanks for your help! and yeah, i'll switch that over
yeah a nil pointer is not going to be a valid database source lol