Skip to content


Subversion checkout URL

You can clone with
Download ZIP


For same key name, HSET then SET is OK, but SET then HSET is ERROR #1503

getify opened this Issue · 4 comments

2 participants


Redis seems to be ok with changing a key that was already a hash back into a non-hash (string) value, as:

HSET mykey foo "bar"
(integer) 1
SET mykey "blah"

But the reverse direction of type change throws an error:

SET mykey "blah"
HSET mykey foo "bar"
WRONGTYPE Operation against a key holding the wrong kind of value

Is there some other way to "convert" a string key into a hash key? I don't want to DEL and then HSET because that's not guaranteed atomic and could be a race-condition.

This seems like a strange gotcha that you can go in one direction of type change, but not the other. I couldn't find any explanation in the docs of it.



That's acceptable behavior. You also can't SADD mykey newElement or ZADD or LPUSH etc.

Your SET is overriding the type while HSET can't override a non-hash type.

The solution to make delete-then-set atomic is make a short lua script doing your DEL followed by your HSET.


That's acceptable behavior.

According to whom? It's not acceptable to me. I couldn't find anywhere that such a gotcha is documented as "by design".

while HSET can't override a non-hash type.

You've diagnosed what I already knew. But you haven't explained WHY that's the case. Is there some reason (other than it being lack of a feature functionality) that SET can override type but HSET (and SADD, ZADD, etc) cannot?

The solution to make delete-then-set atomic is make a short lua script

I wish you were joking. That's one of the least helpful tech answers I've received in awhile. It amounts to "you shouldn't be doing this, and since you are, go try this crazy other hack and leave me alone".

I'm not a lua programmer. I'm not even a redis power-user. I started using redis from node recently, since it seemed to be a nice lightweight key/value store for persistence.

But even if I knew lua, that's akin in my mind to being told "SQL doesn't support some ___ basic operation, go write a stored procedure". I don't use stored procedures in databases because they separate important code from the rest of the application, making maintenance harder. So I'm also not going to drop code into a "short lua script", even if I knew how.


FYI, the reason I was trying to do this is I need to use SETNX to create/reserve the initial record at the chosen generated unique key, and then update it with hash fields/values. Unfortunately, HSETNX only applies its "unique set" behavior to the fields, not to the hash key itself, so I can't do HSETNX and then HSET. All I've figured out so far was to try SETNX followed by HSET, but this is failing too.


I'm sorry, I suppose this thread just has gone off the deep-end and apparently it's my fault. I'll try again from a different angle in a different thread.

@getify getify closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.