redis-om-python icon indicating copy to clipboard operation
redis-om-python copied to clipboard

Handling optional fields and None values for HashModels

Open kantholtz opened this issue 1 year ago • 0 comments

I was unable to find an explanation of how None values are handled. I've looked through the docs and hashmodel tests and did not find any explicit things regarding None values.

>>> from redis_om import HashModel, Field, Migrator                                                  
...                                                                                                  
... class A(HashModel):                       
...     x: str | None = Field(index=True)                                                                                                                                                                 
...                                                                                                  
... a = A(x=None)                                                                                                                                                                                         
>>> from redis_om import HashModel, Field, Migrator                                                  
...                                                                                                                                                                                                       
... class A(HashModel):
...     x: str | None = Field(index=True)     
...                                               
... a = A(x=None)
>>> a.save()
A(pk='01H9N93N9RFHHCQZS59P91572Y', x=None)
>>> A.get(a.pk)
A(pk='01H9N93N9RFHHCQZS59P91572Y', x='')

I think this is an unexpected behaviour (even if it makes sense when thinking from a "low-level" redis pov). It is also inconsistent with redis-py where saving a None value leads to str(None) being called which saves the literal string 'None' to redis.

See also https://github.com/redis/redis-om-python/issues/38, https://github.com/redis/redis-om-python/issues/254

I've opened this issue to discuss a fix (the other open issues have been open for a very long time) and I'm happy to help out code-wise. I have a few questions:

  1. Is this intended behaviour? If so, a disclaimer should be added to the documentation and this issue can be closed.
  2. It works for JsonModel instances. Is the use of the HashModel discouraged in general?
  3. If it should be changed, how should it be approached? Obviously, assuming an empty string is always meant to be None is a bad idea. Maybe a "magic" (unlikely, random) string could be used instead?

kantholtz avatar Sep 06 '23 13:09 kantholtz