{
  "id": "python/sqlalchemy-stale-data-issue",
  "signature": "sqlalchemy.orm.exc.StaleDataError: UPDATE statement on table 'users' expected to update 1 row(s); Only 0 were matched.",
  "signature_zh": "sqlalchemy.orm.exc.StaleDataError：对表 'users' 的 UPDATE 语句预期更新 1 行；但只匹配了 0 行。",
  "regex": "sqlalchemy\\.orm\\.exc\\.StaleDataError:\\ UPDATE\\ statement\\ on\\ table\\ 'users'\\ expected\\ to\\ update\\ 1\\ row\\(s\\);\\ Only\\ 0\\ were\\ matched\\.",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Optimistic concurrency control: row was modified or deleted by another transaction.",
  "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": "Ignoring the error and retrying without refresh.",
      "why_fails": "Stale object leads to further inconsistencies.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Disabling versioning entirely.",
      "why_fails": "Loses concurrency protection; data corruption possible.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Refresh object before update.",
      "success_rate": 0.9,
      "how": "session.refresh(user)\nuser.name = 'new_name'\nsession.commit()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use version counter in model.",
      "success_rate": 0.85,
      "how": "class User(Base):\n    __tablename__ = 'users'\n    id = Column(Integer, primary_key=True)\n    version_id = Column(Integer, nullable=False)\n    __mapper_args__ = {'version_id_col': version_id}",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}