How to Solve RuntimeError:Working outside of application context FLASK error

Currently, am learning how to create api using python and flask framework.

while on the process of creating db from sql alchemy i got an error of runtime error working outside of application context. so i solve by adding the codes.

Before i was running these commands which gave me an error:

    from application import db

    db.create_all()

When i went to stack-overflow and youtube i found out i should run the following commands

    from application import app, db

    app.app_context().push()

    db.create()

then abracadabra it worked 

This is solved by the following code on your cli or cmd:

>>> from application import app, db

>>> app.app_context().push()

>>> db.create_all()

The error looks like the following:

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Users/obadiagilbert/Desktop/Test_Python/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 868, in create_all

    self._call_for_binds(bind_key, "create_all")

  File "/Users/obadiagilbert/Desktop/Test_Python/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 839, in _call_for_binds

    engine = self.engines[key]

  File "/Users/obadiagilbert/Desktop/Test_Python/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 628, in engines

    app = current_app._get_current_object()  # type: ignore[attr-defined]

  File "/Users/obadiagilbert/Desktop/Test_Python/venv/lib/python3.10/site-packages/werkzeug/local.py", line 513, in _get_current_object

    raise RuntimeError(unbound_message) from None

RuntimeError: Working outside of application context.


Check out this video which helped me.





Comments