{
  "id": "python/flask-sqlalchemy-detached-instance",
  "signature": "sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <User> is not bound to a Session; lazy load operation of attribute 'posts' cannot proceed",
  "signature_zh": "SQLAlchemy ORM 异常：DetachedInstanceError：父实例 <User> 未绑定到会话；属性 'posts' 的懒加载操作无法进行。",
  "regex": "sqlalchemy\\.orm\\.exc\\.DetachedInstanceError:\\ Parent\\ instance\\ <User>\\ is\\ not\\ bound\\ to\\ a\\ Session;\\ lazy\\ load\\ operation\\ of\\ attribute\\ 'posts'\\ cannot\\ proceed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "在 Flask 会话关闭后访问了 ORM 对象的懒加载属性，导致 DetachedInstanceError。",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Flask 会话关闭后访问了 ORM 对象的懒加载属性，导致 DetachedInstanceError。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "如果关联数据很多，会降低性能，且可能遗漏。",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "重新附加可能引发其他冲突，且需要额外代码。",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from sqlalchemy.orm import joinedload\nuser = db.session.query(User).options(joinedload(User.posts)).first()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "with app.app_context():\n    user = db.session.get(User, 1)\n    posts = user.posts  # 访问后再关闭会话",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}