python config_error ai_generated true

RuntimeError: 当前 Flask 应用未注册到此 'SQLAlchemy' 实例。

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-06-06首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

    Instance still lacks the app binding.

  2. 85% 失败

    Creates two instances; models attach to the wrong one.