WRONGTYPE: The key holds a different data type
The command expects one Redis data type and the key holds another: a list command on a string key, for example.
- Error prefix
WRONGTYPE
의미
WRONGTYPE means the key exists but holds a different data type than the command needs. Redis keys are not schema-bound: strings, lists, sets, sorted sets, hashes and streams share one namespace, and the type is a property of the value, not of the key name. LPUSH on a key that holds a string fails exactly like this.
It is the Redis equivalent of a schema mismatch, and it usually starts as a naming collision: two parts of the application using one key name for two purposes, or an older writer that created the key as a string before a feature switched it to a hash. The investigation starts with TYPE, which tells you what you are actually holding.
일반적인 원인
- Two code paths use the same key name for different types, so whichever creates the key first wins.
- A migration or refactor changed the type a feature uses, and keys from the previous format are still there.
- A deserializer or ORM hands the command the wrong field name, pointing at a string key next to the collection key.
Find the cause
TYPE user:1000 OBJECT ENCODING user:1000 TTL user:1000 # Scan the prefix to spot who else is writing nearby: SCAN 0 MATCH user:1000* COUNT 100
Commands and the types they require
| GET, SET, INCR, APPEND | 문자열 |
|---|---|
| LPUSH, RPUSH, LRANGE, LPOP | 목록 |
| HSET, HGET, HGETALL | 해시 |
| SADD, SMEMBERS, SINTER | 셋 |
| ZADD, ZRANGE, ZRANK | 정렬된 셋 |
| XADD, XREAD, XGROUP | 스트림 |
해결 방법
-
Check the actual type and encoding before changing any code:
TYPE user:1000 OBJECT ENCODING user:1000
-
Rename one side of the collision so each purpose has its own key:
RENAME user:1000 user:1000:profile-backup
-
Once nothing else writes to the old key, delete or rebuild it with the right commands:
DEL user:1000 HSET user:1000 name alice plan pro
Other ways to handle it
Where the type genuinely varies, encode the type in the key name (session:1000:hash, queue:jobs:list) so collisions are visible in code review. For legacy data, read the old value once with GET, convert it, write it back with the right command, and remove the old key in the same deploy.
안전하게 재현하세요
redis-cli SET user:1000 alice redis-cli LPUSH user:1000 tasks # (error) WRONGTYPE Operation against a key holding the wrong kind of value redis-cli TYPE user:1000 # string
질문
How do I see a key's type?
TYPE key prints it: string, list, set, zset, hash, stream, bitmap or vectorset. OBJECT ENCODING adds the internal encoding, which explains behavior differences inside one type.
Why is Redis strict about this?
Commands are typed per data structure. Silently coercing types would make every writer depend on the order in which readers and writers happened to run.
Can one key hold two types?
No. A key has exactly one value with one type. Put the second structure under a different key.
관련 오류
Conversion ErrorDuckDB: The value cannot be cast to the target type42804PostgreSQL: 데이터 타입 불일치SQLITE_MISMATCHSQLite: datatype mismatch
More data types and commands errors
ERR unknown commandRedis does not know this commandERR wrong number of argumentsArgument count is wrongNOSCRIPTScript SHA1 is not cachedEXECABORTTransaction discarded after an errorBUSYServer is blocked by a long scriptERR invalid expire timeExpire value is out of range
코드와 메시지 텍스트의 출처는 다음과 같습니다 Redis documentation and source code. 전체 목록은 Redis error list 또는 모든 데이터베이스 오류 검색.
Work on Redis in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for Redis and the other databases you use.
이 페이지의 텍스트와 데이터: CC BY 4.0, 출처: SQLly (sqlly.app). 코드와 SQL 스니펫: MIT. 라이선스 세부 정보 인용된 오류 메시지 텍스트의 권리는 해당 데이터베이스 공급업체에 있습니다.