{
  "id": "python/pytest-sqlalchemy-session-not-bound",
  "signature": "sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper[User(users)] or this Session.",
  "signature_zh": "sqlalchemy.exc.UnboundExecutionError: 无法为 Mapper Mapper[User(users)] 或此 Session 定位绑定。",
  "regex": "sqlalchemy\\.exc\\.UnboundExecutionError:\\ Could\\ not\\ locate\\ a\\ bind\\ configured\\ on\\ mapper\\ Mapper\\[User\\(users\\)\\]\\ or\\ this\\ Session\\.",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "A SQLAlchemy Session was created without an engine (or sessionmaker bound to engine) and the test fixture yields a session that is later used against an unbound mapper.",
  "root_cause_type": "generic",
  "root_cause_zh": "创建 SQLAlchemy Session 时未传入 engine（或未将 sessionmaker 绑定到 engine），测试 fixture 产出的 session 在之后对未绑定的 mapper 使用时抛出异常。",
  "versions": [
    {
      "version": "2.0.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Rebinding after the session has already executed queries or checked out connections raises InvalidRequestError; also fragile with scoped_session.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "session.connection() requires a bind in the first place; it raises the same UnboundExecutionError.",
      "fail_rate": 0.92,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.96,
      "how": "from sqlalchemy.orm import sessionmaker\n\n@pytest.fixture\ndef db(engine):\n    Session = sessionmaker(bind=engine)\n    session = Session()\n    yield session\n    session.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "from sqlalchemy.orm import scoped_session, sessionmaker\n\n@pytest.fixture\ndef db(engine):\n    factory = sessionmaker(bind=engine)\n    Session = scoped_session(factory)\n    yield Session()\n    Session.remove()",
      "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-08-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}