CoreyMSchafer / code_snippets

MIT License
10.36k stars 17.55k forks source link

[Python/Flask_Blog] apply app_context to solve RunTimeError #207

Open nimstepf opened 1 year ago

nimstepf commented 1 year ago

Patch for RuntimeError in Python/Flask_Blog:

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed the current application. 
To solve this, set up an application context with app.app_context(). See the documentation for more information.

Since Flask-SQLAlchemy 3.0, all access to db.engine (and db.session) requires an active Flask application context. db.create_all uses db.engine, so it requires an app context. The solution was proposed by davidism on stackoverflow (https://stackoverflow.com/questions/73961938). More information can be found in the docs (https://flask.palletsprojects.com/en/2.3.x/appcontext/)

Changes Made:

Thank you for your exceptional tutorials, as they have greatly enhanced my understanding in Python. Please review and merge this pull request at your earliest convenience. Thank you!