WRONGTYPE Operation against a key holding the wrong kind of value php
Asked Answered
P

8

339

Hi I am using Laravel with Redis .When I am trying to access a key by get method then get following error "WRONGTYPE Operation against a key holding the wrong kind of value"

I am using following code to access the key value -

i use this code for get data from redis

$values = "l_messages";
$value = $redis->HGETALL($values);
print($value);
Picco answered 21/6, 2016 at 19:28 Comment(0)
B
935

Redis supports 6 data types. You need to know what type of value that a key maps to, as for each data type, the command to retrieve it is different.

Here are the commands to retrieve key value(s):

  • if value is of type string -> GET <key>
  • if value is of type hash -> HGET or HMGET or HGETALL <key>
  • if value is of type lists -> lrange <key> <start> <end>
  • if value is of type sets -> smembers <key>
  • if value is of type sorted sets -> ZRANGEBYSCORE <key> <min> <max>
  • if value is of type stream -> xread count <count> streams <key> <ID>. https://redis.io/commands/xread

Use the TYPE command to check the type of value a key is mapping to:

  • type <key>
Biometry answered 8/6, 2017 at 20:27 Comment(5)
So when you use the methods that write/read byte[]'s directly, which of those is being used?Tedtedd
why I can't only use hget for hash type?Holmgren
@Holmgren you can use HGET and specify the "field in the hash stored at key". 'HGET <key> <field>' or HGETALL to see all the fieldsArgosy
Great answer! I don't think I have seen this explained as clearly and simply as this on the Redis website or in their documentation.Populous
You saved my day too, thanks :). And for who is using bullmq, to look at jobs in a queue you should try keys * and HGETALL key to see what you sent to the queue.Leigh
P
26

This error says that you are trying to push a wrong value into the key, which means that there already exists same key but with different data structure.

To get all the keys do this in the redis cli

keys *

This should display all the keys Now to get the type of value the key stores, do

type <key>

so it says what value you can push into the key. In my case the type was string (using set) and i was trying to use the key as list

Pasteboard answered 10/5, 2021 at 11:7 Comment(0)
A
13

This error means that the value indexed by the key "l_messages" is not of type hash, but rather something else. You've probably set it to that other value earlier in your code. Try various other value-getter commands, starting with GET, to see which one works and you'll know what type is actually here.

Arabia answered 21/6, 2016 at 19:48 Comment(2)
Actually, print($redis->TYPE($values)); will tell it to your without the guesswork ;)Camporee
Great :) Didn't know this command, never had to use it. Here's the documentation: redis.io/commands/typeArabia
A
4

I faced this issue when trying to set something to redis. The problem was that I previously used "set" method to set data with a certain key, like

$redis->set('persons', $persons)

Later I decided to change to "hSet" method, and I tried it this way

foreach($persons as $person){
    $redis->hSet('persons', $person->id, $person);
}

Then I got the aforementioned error. So, what I had to do is to go to redis-cli and manually delete "persons" entry with

del persons

It simply couldn't write different data structure under existing key, so I had to delete the entry and hSet then.

Adna answered 22/9, 2020 at 6:21 Comment(0)
A
3

This isn't PHP but I had a similar issue in C# and used this page as a reference to resolve the issue.

My .Net Core 3.1 web server's IDistributedCache.SetStringAsync was actually storing the data as a hash instead of a string; I confirmed this using KeyTypeAsync. Needless to say this was extremely confusing. It seems to be storing the configuration DistributedCacheEntryOptions as part of the hash with {data} being the sub-key of the original string.

To resolve this issue you need to read it as a hash and access the sub-key manually instead of reading it as a string. See following code:

var str = (await redisConnection.GetDatabase().HashGetAllAsync("stringKey"))?.FirstOrDefault(ele => ele.Name == "data")?.Value?.ToString();

EDIT: the order of the sub-hash keys is no longer consistent in recent versions of REDIS, so you can't rely on the last item being the actual data anymore. Make sure you match on ele.Name as above to avoid unexpected errors.

Anywise answered 12/9, 2022 at 4:29 Comment(0)
U
1

I faced this issue and came to find out that there was another already existing key with the same name but with different types. The already existing key was a string while I was expecting a hash.

Upholstery answered 23/1 at 10:10 Comment(0)
R
0

WRONG DATABASE INDEX

Sometimes it's because you are in a wrong db number. I had a same problem and changed the db to the right one, there was no problem and it worked!. I suggest you before HGETALL or whatever you want, select the correct db or at least considering which db you are in.

Rutan answered 18/5, 2022 at 6:18 Comment(0)
N
0

I got this error by attempting to fetch a value using plain GET that was originally stored with JSON.SET. I just needed to ensure I was consistently using the correct commands.

This was in Node.js, so the fix was to go from:

await client.get(key);

To:

await client.json.get(key);
Neufer answered 13/3, 2023 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.