{
  "id": "python/sqlalchemy-sqlite-foreign-key-off",
  "signature": "sqlite3.IntegrityError: FOREIGN KEY constraint failed",
  "signature_zh": "sqlite3.IntegrityError: 外键约束失败",
  "regex": "sqlite3\\.IntegrityError:\\ FOREIGN\\ KEY\\ constraint\\ failed",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "SQLite by default does not enforce foreign key constraints unless PRAGMA foreign_keys=ON is set; but if it's off, referential integrity can be violated, and when turned on, existing bad data causes errors.",
  "root_cause_type": "generic",
  "root_cause_zh": "SQLite 默认不强制执行外键约束，除非设置 PRAGMA foreign_keys=ON；但如果关闭，引用完整性可能被违反，当打开时，现有错误数据会导致错误。",
  "versions": [
    {
      "version": "1.4.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "2.0.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Data inconsistency persists.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "May lose data and doesn't fix the root cause.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Enable foreign key enforcement per connection: from sqlalchemy import event; @event.listens_for(engine, 'connect') def set_sqlite_pragma(dbapi_connection, connection_record): cursor = dbapi_connection.cursor(); cursor.execute('PRAGMA foreign_keys=ON'); cursor.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "Clean up orphaned records before enabling FK constraints.",
      "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-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}