{
  "id": "python/flask-sqlalchemy-detached-instance-error",
  "signature": "sqlalchemy.orm.exc.DetachedInstanceError: Instance is not bound to a Session; attribute refresh operation cannot proceed",
  "signature_zh": "SQLAlchemy ORM异常：实例未绑定到会话；属性刷新操作无法继续",
  "regex": "sqlalchemy\\.orm\\.exc\\.DetachedInstanceError:\\ Instance\\ is\\ not\\ bound\\ to\\ a\\ Session;\\ attribute\\ refresh\\ operation\\ cannot\\ proceed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Accessing lazy-loaded attributes on a SQLAlchemy object after its session has been closed, common in Flask when sessions are removed at request end.",
  "root_cause_type": "generic",
  "root_cause_zh": "在会话关闭后访问SQLAlchemy对象的懒加载属性，常见于Flask在请求结束时移除会话。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Setting expire_on_commit=False can prevent expiration but still fails if session is closed.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Re-attaching the object with db.session.add() fails because the instance is already persistent.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use joined loading: query with .options(joinedload(MyModel.relationship)) to eagerly load data.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Access all needed attributes within the session scope, e.g., in the view function before returning.",
      "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.84,
  "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": []
}