Flask deployment Crash
I recently deployed a Flask backend app to railway, and It crashed. This is the main.py:
==> crashed at line 35: db.init_app(app)
135 Replies
Project ID:
c6894929-c040-4842-8c83-4516d57a14fe
c6894929-c040-4842-8c83-4516d57a14fe
deploy logs please https://bookmarklets.up.railway.app/log-downloader/
Locally, its working
my initial thoughts are to ask you if you have setup all those environment variables in railway too?
The debug, testing, and other ones are inside a config.py
for example you use
SQLALCHEMY_DATABASE_URI
in your code, but on railway you are using DATABASE_URL
are you perhaps using DATABASE_URL
in the config.py file instead?yes: SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
his PORT is not setup either, Issakass i thought i told you about that.
doesn't need to be
the port has no problems
they are using gunicorn and that will use the automatically injected PORT variable that railway injects during runtime
ah
I see
then int(os.getenv())
nope, that's not the issue either
I have a configuration file, it has 3 config classes, for Development, Testing and Production. I am setting up app.config for the sepecific environment,
issa, can you please edit your original message to wrap your code in triple backticks?
please edit the original message
this is from postman:
yeah that's the html page from railway that is shown when your app does not respond to a request, in this case because the app has crashed
yes, it crashed
do you want also to share the config.py?
can you send your config.py file?
When developing, its working well!
All my problem is when deploying
that is a good to know, but keep in mind that does not rule out a code issue, this is most definitely a code issue
I tried to deploy railway flask template, it works. But adding all these extensions and configurations, it crashes
yes this is a code issue, I'm still thinking on what could be wrong though
File "/app/main.py", line 35, in <module>
db.init_app(app)
return int(port)
ValueError: invalid literal for int() with base 10: ''
a very weird error
I want also to connect it to postgresql service, but haven't tried
can you show us the code in db.init_app
you mean that extensions.py that contains db?
anyone here??
I think your database url has some issues.
Can you confirm it has port value in the string?
the database is working locally very well
I understand
Most probably it's your database string.
Please confirm there's a port value in it.
which one?
or in the railway?
In the railway environment
DATABASE_URL
I want the postgresql service to work with the backend service
How to share variables between backend and postgresql?
I want you to check the DATABASE_URL which is setup in environment variables and confirm that it contains PORT
no
postgresql://:@:/
How are you connecting postgres then?
Where is your postgres service hosted?
I haven't yet tried to connect to postgrs
in railway
this is my app:
Alright
Go to postgres service and copy it's PRIVATE_URL
and put it in your backend DATABASE_URL
why are you angryπ
should I copy also PGUSER, PGPORT,...
Did I say something wrong?
we never want to copy paste credentials from other services like that, always use reference variables, as shown here https://docs.railway.app/guides/variables#referencing-another-services-variable
so what to do now?
please look at that docs link for how you want to setup your database url service variable
i referenced it
can you show me how you've done that?
the db url referenced from psql
maybe let's not send that in a public chat
ok
are you using a reference variable for your DATABASE_URL service variable on railway
yes. I added a new variable in Backend service, and added reference from psql
show me the raw value
DATABASE_URL=${{Postgres.DATABASE_URL}}
It works well now. There is one thing only now
how to load my database??
and keep updating it without affecting its data
looks like you need to run a migration
Yes, that right
but before that, I want to remove all existing data
well you have no tables so therefore no data
In development and testing, I am working with Sqlite
should i create tables manually?
Or I can import from existing ones
you should run a migration
does the migration works eveny if developing with sqlite
??
or the migration doesn't depend on any database, it depends on sqlalchemy and models??
that's correct
what command would you run to perform the migration?
followed by
and where do those commands pull the database credentials from?
since they connect to the database after all, they need to get the database credentials from somewhere
couldnt understand correctly
not too sure how I could really say that in a more easy to understand way
for example if I type flask to the terminal: Commands:...
db Perform database migrations.
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.
flask db for performing db migrations
yes I get that, but the db command connects to the database, where is it getting the database credentials so it knows what database to connect and authenticate with
I think from config.py
here i am specifiying SQLALCHEMY_DATABASE_URI for each environment
please confirm that, having to guess on such things is going to make your job as a developer exponentially harder, and my job helping you even harder
here, I am providing SQLALCHEMY_DATABASE_URI, and the migrations should work on it
I can see that, but is that what database url the flask db command will use to connect to and run the migrations?
yes
creates a migration directory
okay so have you tried to run these commands locally?
yes
it creates a migrations folder locally
are you running in production locally so that the migrations are ran on railways postgres database?
I worked with this, when defining new models or changes to db, then I migrate and upgrade it
no, the migrations are ran locally
you need to perform the migrations on the postgres database on railway
you may need to run your app in production locally so that your code connects to the database on railway instead of sqlite
I'll tru
try*
did you know you can edit your messages on discord
so I may change FLASK_ENV to production, and run migration??
that sounds correct to me
no, I'm new to using discord
sorry
no worries, I am new to discord too believe it or not lol
I am getting: RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
have you set it in your .env file
so I must set Database_url in .env?
correct
so whenever I want to change things, I change the .env?
I was thinking that .env must be static and not changes
migrated successfully
should i add references to them in backend??
did you touch the service variables on the database service??
no
railway is telling you that you have removed one of those variables
all are available
okay either way it doesn't really matter, the data tab is not reliable
they are shown
please confirm the tables are there with software like dbgate
i was logged out
I logged in an the tables exists
okay good enough
so yeah tables are there, job done
so now the app should work?
Thank you very much
I assume so, I have no way to say for certain
Thank you!!!
you have the flask environment set to production on railway right?
Yes
can I set free domain also?
of course
that's done in the service settings
show some custom domain for frontend and backend
example
do you own your own domain name?
no
I tried a POST request to the api and got 500 server error, why?
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
what do the deployment logs say
2024-01-15 17:28:54.528 UTC [9698] ERROR: relation "users" does not exist at character 217
could it be due some conflict between sqlalchmey and psql?
do you have any users in the database?
which one?
the users table is empty
seems like your code doesnt know what to do when the users table is empty
but now this thread is divulging more into coding help, and unfortunately thats not something we can really offer here
this is the user model, could the error because of the relations between tables?
im sorry but as stated above, we dont really offer coding help here unfortunately, maybe ask in a sqlalchemy forum?
Ok, i'll try. Thank you!
your app is running on railway now, you just have to work out a few kinks in your code!
Thanks!
no problem!
Hello
I have a very small question
hello
do you remember when I explicitly added FLASK_ENV to backend??
locally?
no, I added it explicitly to railway variables
and use it like: flask_env = os.getenv("FLASK_ENV")
yeah?
I run the command: railway variables and got:
and more also
is it better to use RAILWAY_ENVIRONMENT instead??
no i would stick with the covenantal
FLASK_ENV
since you arent using railways environment system anywayok, thanks!
no prob!