R
Railway•16mo ago
intro

How would I connect my project with the SQL database ?

I have a model for users and I would like to populate my production DB with them. How can I do that?
60 Replies
Percy
Percy•16mo ago
Project ID: 3b4c3c14-6b11-4e8a-8f52-bb11ff2f2554
intro
introOP•16mo ago
3b4c3c14-6b11-4e8a-8f52-bb11ff2f2554
Brody
Brody•16mo ago
how would you normally seed the database with that .db file?
intro
introOP•16mo ago
flask seed all
Brody
Brody•16mo ago
and where does flask pull the credentials for that from?
intro
introOP•16mo ago
i would assume this here has something to do with it
intro
introOP•16mo ago
i would initiate a database instance with flask db init then flask db migrate, flask db upgrade then flask seed all
intro
introOP•16mo ago
the seeds/__init__ creates the seed all command that flask is using
Brody
Brody•16mo ago
is that for postgres or sqlite? im not seeing anything that would connect to postgres
intro
introOP•16mo ago
This is for SQLAlchemy I think ? This all works locally, I just also need my data to be in my production
Brody
Brody•16mo ago
is SQLAlchemy for postgres?
intro
introOP•16mo ago
SQLAlchemy is a ORM
Brody
Brody•16mo ago
I know that much is it an orm for postgres or sqlite
intro
introOP•16mo ago
Postgres
Brody
Brody•16mo ago
okay perfect
intro
introOP•16mo ago
I have hooked a project that used SQLAlchemy with a Postgres DB on "render" before and it worked
Brody
Brody•16mo ago
but I'm not seeing anything in your code that connects to postgres?
intro
introOP•16mo ago
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?
Brody
Brody•16mo ago
but I don't see anything in your code that uses a DATABASE_URL environment variable?
intro
introOP•16mo ago
it's in my config.py
Brody
Brody•16mo ago
okay and where are you using that to connect to the database
intro
introOP•16mo ago
in my main.py
Brody
Brody•16mo ago
right but where are you initialising the connection to the database
intro
introOP•16mo ago
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)
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)
Brody
Brody•16mo ago
okay well I'm just gonna take your word for it and assume your code will properly use a DATABASE_URL variable if present
intro
introOP•16mo ago
yes it should
Brody
Brody•16mo ago
show me a screenshot of the backend's service variables please
intro
introOP•16mo ago
Brody
Brody•16mo ago
perfect
intro
introOP•16mo ago
doesn't seem to work though ^^
intro
introOP•16mo ago
shouldn't i see my tables here?
Brody
Brody•16mo ago
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
intro
introOP•16mo ago
: /
Brody
Brody•16mo ago
this would be an issue with your app/code
intro
introOP•16mo ago
but i am able to seed locally just fine
intro
introOP•16mo ago
does main.py need to be __init__.py ?
Brody
Brody•16mo ago
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
intro
introOP•16mo ago
I deleted my db instance and migrations folder and rerun migrations and seeds with no issue
Brody
Brody•16mo ago
but what does that seed?
intro
introOP•16mo ago
my local db
Brody
Brody•16mo ago
that looks like an sqlite database to me?
intro
introOP•16mo ago
yes it is i think i know whats wrong it's the env variable in the postgres instance on railway
Brody
Brody•16mo ago
i tried very hard to make sure your code was connecting to a postgres database
intro
introOP•16mo ago
i set FLASK_APP to a wrong value, i changed it and now the seeding worked
Brody
Brody•16mo ago
DATABASE_URL is the standard name for that variable, nothing wrong there the database on railway seeded?
intro
introOP•16mo ago
hmm no, it actually seemes like the seeding threw an error
intro
introOP•16mo ago
Brody
Brody•16mo ago
dont you need to do a migration to prepare the tables before you seed?
intro
introOP•16mo ago
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?
Brody
Brody•16mo ago
im not sure tbh, i dont do much with databases, just make sure all these commands are ran with railway run
intro
introOP•16mo ago
they did
Brody
Brody•16mo ago
then thats unfortunately where my knowledge on the subject runs out, i belive you will get this though, youre close
intro
introOP•16mo ago
ok i'll try to consult chatgpt on this ^^ thank you for your help thus far!
Brody
Brody•16mo ago
no problem 🙂
intro
introOP•16mo ago
@Brody oh one thing, does REACT_APP_BASE_URL have to be the url of my backend service?
intro
introOP•16mo ago
i guess not when it's REACT_APP... nevermind ^^
Brody
Brody•16mo ago
haha yep
intro
introOP•16mo ago
@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.
Brody
Brody•16mo ago
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
intro
introOP•16mo ago
like this?
Want results from more Discord servers?
Add your server