{
  "id": "database/redis-lua-script-execution-error",
  "signature": "redis.exceptions.ResponseError: Error running script (call to f_abcdef123456): @user_script:1: WRONGTYPE Operation against a key holding the wrong kind of value",
  "signature_zh": "redis.exceptions.ResponseError: 运行脚本时出错（调用 f_abcdef123456）：@user_script:1: WRONGTYPE 操作针对持有错误类型值的键",
  "regex": "Error running script \\(call to f_[a-f0-9]+\\): @user_script:\\d+: WRONGTYPE Operation against a key holding the wrong kind of value",
  "domain": "database",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A Redis Lua script attempted to perform an operation (e.g., LPUSH on a string key) that is incompatible with the data type stored at the key, causing a WRONGTYPE error.",
  "root_cause_type": "generic",
  "root_cause_zh": "Redis Lua 脚本尝试对键执行与存储的数据类型不兼容的操作（例如对字符串键执行 LPUSH），导致 WRONGTYPE 错误。",
  "versions": [
    {
      "version": "Redis 7.2.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Redis 6.2.14",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Redis Stack 7.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Flushing all keys with FLUSHALL to clear the data type mismatch",
      "why_fails": "This deletes all data, which is an extreme measure; the script will still fail if the same key is recreated with the wrong type.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the error and retrying the script",
      "why_fails": "The error is deterministic; retrying without fixing the key type will produce the same error every time.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using EVAL instead of EVALSHA to force script recompilation",
      "why_fails": "The error is not due to script caching; it's a runtime type mismatch. EVAL will still fail with the same error.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).",
      "success_rate": 0.9,
      "how": "Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end",
      "success_rate": 0.85,
      "how": "Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script.",
      "success_rate": 0.8,
      "how": "If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Identify the offending key by running: TYPE <key> in redis-cli. Then either delete the key (DEL <key>) or modify the script to handle the existing type (e.g., use EXISTS to check type before operations).",
    "Update the Lua script to use TYPE command to check the key's type before performing operations, and return an error or handle gracefully: local keyType = redis.call('TYPE', KEYS[1]); if keyType ~= 'list' then return redis.error_reply('Expected list, got ' .. keyType) end",
    "If the key should be a list but is currently a string, convert it by deleting and recreating: DEL mykey; LPUSH mykey value1 value2; then retry the script."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://redis.io/docs/latest/develop/interact/programmability/error-handling/",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-09-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}