{
  "id": "python/flask-sqlalchemy-detachedinstanceerror",
  "signature": "sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x...> is not bound to a Session; attribute refresh operation cannot proceed",
  "signature_zh": "sqlalchemy.orm.exc.DetachedInstanceError：实例 <User at 0x...> 未绑定到 Session；无法继续属性刷新操作",
  "regex": "sqlalchemy\\.orm\\.exc\\.DetachedInstanceError:\\ Instance\\ <User\\ at\\ 0x\\.\\.\\.>\\ is\\ not\\ bound\\ to\\ a\\ Session;\\ attribute\\ refresh\\ operation\\ cannot\\ proceed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Accessing an attribute of an ORM object after the session that loaded it has been closed or committed.",
  "root_cause_type": "generic",
  "root_cause_zh": "在加载 ORM 对象的会话关闭或提交后访问该对象的属性。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This only prevents expiration on commit, not detachment after session close.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The session is already closed, so refresh fails.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "user = session.query(User).options(joinedload(User.posts)).first()\n# use user.posts before session.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Use `db.session` within the request context; avoid manual session.close().",
      "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-06-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}