python config_error ai_generated true

RuntimeError: The current Flask app is not registered with this 'SQLAlchemy' instance.

ID: python/flask-sqlalchemy-no-application-found

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-06-06First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

db = SQLAlchemy() was created in a module but db.init_app(app) was never called, or a different app instance is active.

generic

中文

在模块中创建了 db = SQLAlchemy(),但从未调用 db.init_app(app),或当前激活的是另一个 app 实例。

Workarounds

  1. 95% success
    db = SQLAlchemy()
    def create_app():
        app = Flask(__name__)
        db.init_app(app)
        return app
  2. 90% success
    app = create_app()
    with app.app_context():
        db.create_all()

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Instance still lacks the app binding.

  2. 85% fail

    Creates two instances; models attach to the wrong one.