{
  "id": "python/sqlalchemy-foreign-key-violation",
  "signature": "sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) insert or update on table \"orders\" violates foreign key constraint \"orders_user_id_fkey\"",
  "signature_zh": "sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) 在表 \"orders\" 上插入或更新违反了外键约束 \"orders_user_id_fkey\"",
  "regex": "sqlalchemy\\.exc\\.IntegrityError:\\ \\(psycopg2\\.errors\\.ForeignKeyViolation\\)\\ insert\\ or\\ update\\ on\\ table\\ \"orders\"\\ violates\\ foreign\\ key\\ constraint\\ \"orders_user_id_fkey\"",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Attempting to insert a row with a foreign key value that does not exist in the referenced table.",
  "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": "Disabling foreign key checks temporarily",
      "why_fails": "This can lead to data inconsistency and is not a permanent fix.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the error and retrying",
      "why_fails": "The same violation will occur unless the data is corrected.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the referenced record exists before inserting",
      "success_rate": 0.98,
      "how": "user = session.query(User).filter_by(id=user_id).first()\nif user:\n    order = Order(user_id=user.id)\n    session.add(order)\n    session.commit()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a transaction with rollback on error",
      "success_rate": 0.9,
      "how": "try:\n    session.add(order)\n    session.commit()\nexcept IntegrityError:\n    session.rollback()\n    # Handle the error",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}