{
  "id": "python/flask-sqlalchemy-session-scope",
  "signature": "sqlalchemy.exc.InvalidRequestError: Session is already flushing",
  "signature_zh": "SQLAlchemy 无效请求错误：会话已在刷新中。",
  "regex": "sqlalchemy\\.exc\\.InvalidRequestError:\\ Session\\ is\\ already\\ flushing",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Trying to commit or flush a session while it's already in the process of flushing, often due to nested transactions.",
  "root_cause_type": "generic",
  "root_cause_zh": "在会话已经在刷新过程中尝试提交或刷新，通常由于嵌套事务导致。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Exacerbates nested transaction issues.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "May lose changes and not solve the underlying concurrency issue.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Ensure one commit per request: use Flask-SQLAlchemy's teardown to remove session.\nfrom flask_sqlalchemy import SQLAlchemy\ndb = SQLAlchemy(app)\n# In route: db.session.add(obj); db.session.commit() only once",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Use session.begin_nested() for subtransactions and commit at end.",
      "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-03-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}