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