R
Railway

✋|help

How would I connect my project with the SQL database ?

AAda238/24/2023
I have a model for users and I would like to populate my production DB with them. How can I do that?
AAda238/24/2023
3b4c3c14-6b11-4e8a-8f52-bb11ff2f2554
Bbrody1928/24/2023
how would you normally seed the database with that .db file?
AAda238/24/2023
flask seed all
Bbrody1928/24/2023
and where does flask pull the credentials for that from?
AAda238/24/2023
i would assume this here has something to do with it
AAda238/24/2023
i would initiate a database instance with flask db init then flask db migrate, flask db upgrade then flask seed all
AAda238/24/2023
the seeds/__init__ creates the seed all command that flask is using
Bbrody1928/24/2023
is that for postgres or sqlite? im not seeing anything that would connect to postgres
AAda238/24/2023
This is for SQLAlchemy I think ?
This all works locally, I just also need my data to be in my production
Bbrody1928/24/2023
is SQLAlchemy for postgres?
AAda238/24/2023
SQLAlchemy is a ORM
Bbrody1928/24/2023
I know that much
is it an orm for postgres or sqlite
AAda238/24/2023
Postgres
Bbrody1928/24/2023
okay perfect
AAda238/24/2023
I have hooked a project that used SQLAlchemy with a Postgres DB on "render" before and it worked
Bbrody1928/24/2023
but I'm not seeing anything in your code that connects to postgres?
AAda238/24/2023
I have a DATABASE_URL and SECRET_KEY and SCHEMA set in my .env
wouldn't i need to create env variables in the Postgres DB?
Bbrody1928/24/2023
but I don't see anything in your code that uses a DATABASE_URL environment variable?
AAda238/24/2023
it's in my config.py
Bbrody1928/24/2023
okay and where are you using that to connect to the database
AAda238/24/2023
in my main.py
Bbrody1928/24/2023
right but where are you initialising the connection to the database
AAda238/24/2023
I think it's happening in this part

app.cli.add_command(seed_commands)

app.config.from_object(Config)
app.register_blueprint(user_routes, url_prefix='/api/users')
app.register_blueprint(auth_routes, url_prefix='/api/auth')

db.init_app(app)
Migrate(app, db)

# Application Security
CORS(app)
Bbrody1928/24/2023
okay well I'm just gonna take your word for it and assume your code will properly use a DATABASE_URL variable if present
AAda238/24/2023
yes it should
Bbrody1928/24/2023
show me a screenshot of the backend's service variables please
AAda238/24/2023
Bbrody1928/24/2023
perfect
AAda238/24/2023
doesn't seem to work though ^^
AAda238/24/2023
shouldn't i see my tables here?
Bbrody1928/24/2023
install the railway cli https://docs.railway.app/develop/cli
link your project: railway link
link your service: railway service
run the database seed: railway run flask seed all
run these commands in the backend folder
railway run will run the given command locally, but the command will have access to the variables youve set in the railway service, thus hopefully allowing flask seed to connect to the railway database with the now available DATABASE_URL variable
AAda238/24/2023
: /
Bbrody1928/24/2023
this would be an issue with your app/code
AAda238/24/2023
but i am able to seed locally just fine
AAda238/24/2023
does main.py need to be __init__.py ?
Bbrody1928/24/2023
this is seeding locally, these commands are running on your own computer
your just seeding the database on railway
no, the entrypoint should stay main.py, that is standard
AAda238/24/2023
I deleted my db instance and migrations folder and rerun migrations and seeds with no issue
Bbrody1928/24/2023
but what does that seed?
AAda238/24/2023
my local db
Bbrody1928/24/2023
that looks like an sqlite database to me?
AAda238/24/2023
yes it is
i think i know whats wrong
it's the env variable in the postgres instance on railway
Bbrody1928/24/2023
i tried very hard to make sure your code was connecting to a postgres database
AAda238/24/2023
i set FLASK_APP to a wrong value, i changed it and now the seeding worked
Bbrody1928/24/2023
DATABASE_URL is the standard name for that variable, nothing wrong there
the database on railway seeded?
AAda238/24/2023
hmm no, it actually seemes like the seeding threw an error
AAda238/24/2023
Bbrody1928/24/2023
dont you need to do a migration to prepare the tables before you seed?
AAda238/24/2023
i just ran the railway commands just with db migrate db upgrade and then seed all but i am getting the same error
do i need to drop my schema maybe?
Bbrody1928/24/2023
im not sure tbh, i dont do much with databases, just make sure all these commands are ran with railway run
AAda238/24/2023
they did
Bbrody1928/24/2023
then thats unfortunately where my knowledge on the subject runs out, i belive you will get this though, youre close
AAda238/24/2023
ok i'll try to consult chatgpt on this ^^ thank you for your help thus far!
Bbrody1928/24/2023
no problem 🙂
AAda238/24/2023
@Brody oh one thing, does REACT_APP_BASE_URL have to be the url of my backend service?
AAda238/24/2023
i guess not when it's REACT_APP... nevermind ^^
Bbrody1928/24/2023
haha yep
AAda238/24/2023
@Brody Hey, so I ended up deleting that Postgres DB and creating a new one. I changed the DATABASE_URL to the one of the new DB. Though now when I run railway service and railway run .... (seed etc) i always get this error : / any idea what this could be? I also tried to do the connect steps as shown in the screenshot. I did that with the previous DB as well.
Bbrody1928/24/2023
1. please do not hardcode your database variable, use a reference https://docs.railway.app/develop/variables#reference-variables
2. you forgot the flask part of that command
AAda238/24/2023
like this?

Looking for more? Join the community!

Recommended Posts
Hipaa/socI wanted to confirm if the information on https://railway.app/help is current as the email listed boMemory consumption differenceSame app deployed in docker local and Hw(M1 16gb) consumes 52 mb with no load railway took 74mb can Nextjs 13 Environmental variableHello! I recently installed Nextjs to try out the "new" app directory, because I heard server actioRailway CLI postgresql not installed?I installed the CLI and postgresql on my computer to use pgloader to migrate a mysql db to postgres How do I hook up my frontend correctly./I am able to deploy my backend but my frontend is not working yet. Error: ``` #8 [4/5] RUN npm ruStyling bug on project service usage pageIs there a way I can have Railway restart manually every 2 hours?Pretty self explanatory, I’m running some js codePostgres, Aspnet Core API and Angular WebguiHi guys. I'm new here and getting to know railway! I find it fantastic for us. I am lost. I would lCannot change default provider for environment thats created during GitHub Pull RequestI posted about this a while back (https://discord.com/channels/713503345364697088/112689969354953113expressidkI'm not sure on how to transfer my wiki.js database data to railway.86f37e49-db7c-4b6f-9c3e-71f5447b116a I am either getting a broken wiki.js database that results in Build and deployment is accountable in service consumptionconsider in a context of docker image number of images downloaded and each operation performed in doQuestion about billHello i have a railway acount and i work with my accounting team but they need an tax number identifEnvironment Variable Not foundI am using nodejs as a backend and the config plugin with it to get the access to the Railway envoirERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sockI tried deleting my Railway proc file and getting this error now ERROR: Cannot connect to the DockerNeed wordpress template helpI have this error: Please contact your hosting service and ask them to increase max_input_vars valueCan't login using my emailHi there, I'm trying to login to my account using the magic link, but whenever I do it I get this erDjango Deployment Issue - 503 Error on Railway PlatformHello, I am encountering a 503 error ("Application failed to respond") while deploying my Django apAccessing application results in 502. "upstream connect error or disconnect/reset before headers. "Trying to publish an application to the railway platform. It is a mixture of react, together with soAbout Private networkingHi, I saw Railway has https://docs.railway.app/reference/private-networking Private network domain,