Forums

Flask and flask_sqlalchemy support needed - website down

Hi, I have been maintaining a Flask web app for many years and recently had to make some edits due to the Haggis switch. I have two web apps: one is a basic app that is working fine, but the other, which I'll call MYAPP, is currently down due to an issue with Flask and Flask-SQLAlchemy. I'm completely stuck on this.

Here’s the error log:

2025-01-28 19:01:06,255: [2025-01-28 19:01:06,181] ERROR in app: Exception on /pingtest [GET]
2025-01-28 19:01:06,255: Traceback (most recent call last):
2025-01-28 19:01:06,256:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
2025-01-28 19:01:06,257:     response = self.full_dispatch_request()
2025-01-28 19:01:06,257:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask/app.py", line 1607, in full_dispatch_request
2025-01-28 19:01:06,258:     self.try_trigger_before_first_request_functions()
2025-01-28 19:01:06,258:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask/app.py", line 1654, in try_trigger_before_first_request_functions
2025-01-28 19:01:06,258:     func()
2025-01-28 19:01:06,258:   File "/home/MYAPP/mysite/main.py", line 1859, in create_tables
2025-01-28 19:01:06,259:     db.create_all()
2025-01-28 19:01:06,259:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 946, in create_all
2025-01-28 19:01:06,259:     self._execute_for_all_tables(app, bind, 'create_all')
2025-01-28 19:01:06,259:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 938, in _execute_for_all_tables
2025-01-28 19:01:06,259:     op(bind=self.get_engine(app, bind), **extra)
2025-01-28 19:01:06,260:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 880, in get_engine
2025-01-28 19:01:06,260:     return connector.get_engine()
2025-01-28 19:01:06,260:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 540, in get_engine
2025-01-28 19:01:06,260:     self._sa.apply_driver_hacks(self._app, info, options)
2025-01-28 19:01:06,260:   File "/home/MYAPP/.local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 845, in apply_driver_hacks
2025-01-28 19:01:06,261:     info.database = os.path.join(app.root_path, info.database)
2025-01-28 19:01:06,261: AttributeError: can't set attribute

im also running a python flask and am getting 404s now

@Hydrok9 did you follow https://help.pythonanywhere.com/pages/Flask ?

it worked, thanks but i had to comment my code :

@app.before_first_request
def create_tables():
    db.create_all()

how can i reincorporate this code back to flask app?

Normally for a production site you'd manage the database separately, only migrating it when there are changes. Doing that in a website's startup code is a useful short-cut when doing development, but not something you'd typically keep there in the long term.