[Solve] Either work inside a view function or push an application context
👤 By ss | 📆
Error: RuntimeError: No application found. Either work inside a view function or push an application context.
Problem: I am building my datbase from a app.py script, the script was generarting this error when I would go into python3 using these commands:
$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db
YOU ARE IN DEVELOPMENT MODE
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/react/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1033, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/home/react/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1010, in _execute_for_all_tables
app = self.get_app(app)
File "/home/react/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 981, in get_app
raise RuntimeError(
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
[Solution]: Change my code:
FROM: db = SQLAlchemy()
TO: db = SQLAlchemy(app)