{
  "id": "python/flask-app-context-required",
  "signature": "RuntimeError: The current Flask app is not registered with this 'SQLAlchemy' instance. Did you forget to call 'init_app', or did you create multiple 'SQLAlchemy' instances?",
  "signature_zh": "RuntimeError: 当前 Flask 应用未注册到此 'SQLAlchemy' 实例。是否忘记调用 'init_app'，或创建了多个 'SQLAlchemy' 实例？",
  "regex": "RuntimeError:\\ The\\ current\\ Flask\\ app\\ is\\ not\\ registered\\ with\\ this\\ 'SQLAlchemy'\\ instance\\.\\ Did\\ you\\ forget\\ to\\ call\\ 'init_app',\\ or\\ did\\ you\\ create\\ multiple\\ 'SQLAlchemy'\\ instances\\?",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Flask-SQLAlchemy 实例在应用工厂模式中未正确初始化，导致扩展与应用上下文未绑定。",
  "root_cause_type": "generic",
  "root_cause_zh": "Flask-SQLAlchemy 实例在应用工厂模式中未正确初始化，导致扩展与应用上下文未绑定。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "直接导入 db 对象但未调用 init_app，扩展无法关联到当前应用。",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "在请求处理函数中调用 db.create_all()，但应用上下文未激活。",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "创建多个 SQLAlchemy 实例，导致扩展状态混乱。",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "在应用工厂函数中调用 db.init_app(app)，并确保在请求上下文中使用。",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "使用 with app.app_context(): 包裹数据库操作。",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "定义全局 db = SQLAlchemy()，在工厂中 init_app。",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}