{
  "id": "python/sqlalchemy-lazy-load-outside-session",
  "signature": "sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x...> is not bound to a Session; lazy load operation of attribute 'posts' cannot proceed",
  "signature_zh": "sqlalchemy.orm.exc.DetachedInstanceError: 实例 <User at 0x...> 未绑定到会话；属性 'posts' 的延迟加载操作无法进行",
  "regex": "sqlalchemy\\.orm\\.exc\\.DetachedInstanceError:\\ Instance\\ <User\\ at\\ 0x\\.\\.\\.>\\ is\\ not\\ bound\\ to\\ a\\ Session;\\ lazy\\ load\\ operation\\ of\\ attribute\\ 'posts'\\ cannot\\ proceed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Trying to access a lazy-loaded relationship after the session has been closed or the object has been detached.",
  "root_cause_type": "generic",
  "root_cause_zh": "在会话关闭或对象被分离后，尝试访问延迟加载的关系属性。",
  "versions": [
    {
      "version": "1.4.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "2.0.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Adding a detached object may create duplicate or raise errors; it doesn't restore the original session context.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Merge works but may overwrite changes; also it's not ideal for read-only lazy loads.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Eagerly load relationships before closing the session: user = session.query(User).options(joinedload(User.posts)).first()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "Use session.expire_on_commit=False to keep objects usable after commit.",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-25",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}