Forums

RuntimeError: Working outside of application context

.

from flask_sqlalchemy import SQLAlchemy

and config my app

from flask_app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
database = SQLAlchemy(app)

and created this table

class Comment(database.Model):
    __tablename__ = "InvestmentLedger"

    id = database.Column(database.Integer, primary_key=True)
    UserId = database.Column(database.String(4096))
    Email = database.Column(database.String(4096))
    InvestedAmount = database.Column(database.Integer)
    Date = database.Column(database.DateTime)
    Cost = database.Column(database.Integer)
    TradingPlatformId = database.Column(database.String(4096))

and successfully run this command

from flask_app import database

but when i try to run this command

database.create_all()

i get this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/teamip/mysite/env/lib/python3.9/site-packages/flask_sqlalchemy/extension.py", line 868, in create_all
    self._call_for_binds(bind_key, "create_all")
  File "/home/teamip/mysite/env/lib/python3.9/site-packages/flask_sqlalchemy/extension.py", line 839, in _call_for_binds
    engine = self.engines[key]
  File "/home/teamip/mysite/env/lib/python3.9/site-packages/flask_sqlalchemy/extension.py", line 628, in engines
    app = current_app._get_current_object()  # type: ignore[attr-defined]
  File "/home/teamip/mysite/env/lib/python3.9/site-packages/werkzeug/local.py", line 513, in _get_current_object
    raise RuntimeError(unbound_message) from None
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

please, let me know what i'm doing wrong here..

It looks like you didn't instantiate the app (there should be something like app = Flask(__name__) in your code.