# redis.exceptions.ResponseError: 运行脚本时出错（调用 f_abcdef123456）：@user_script:1: WRONGTYPE 操作针对持有错误类型值的键

- **ID:** `database/redis-lua-script-execution-error`
- **领域:** database
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

Redis Lua 脚本尝试对键执行与存储的数据类型不兼容的操作（例如对字符串键执行 LPUSH），导致 WRONGTYPE 错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Redis 7.2.4 | active | — | — |
| Redis 6.2.14 | active | — | — |
| Redis Stack 7.2.0 | active | — | — |

## 解决方案

1. ```
   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).
   ```
2. ```
   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
   ```
3. ```
   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.
   ```

## 无效尝试

- **Flushing all keys with FLUSHALL to clear the data type mismatch** — This deletes all data, which is an extreme measure; the script will still fail if the same key is recreated with the wrong type. (95% 失败率)
- **Ignoring the error and retrying the script** — The error is deterministic; retrying without fixing the key type will produce the same error every time. (100% 失败率)
- **Using EVAL instead of EVALSHA to force script recompilation** — The error is not due to script caching; it's a runtime type mismatch. EVAL will still fail with the same error. (90% 失败率)
