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

JsonModel: find not working with Optional and no default

Open gam-phon opened this issue 2 years ago • 0 comments

Hello,

In JsonModel, find not working with class that has Optional and no default value, whereas HashModel is working

Check out below code:

class CustomerHash1(HashModel):
    name: str = Field(index=True)
    bio: Optional[str]


class CustomerHash2(HashModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio")


class CustomerHash3(HashModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio", default="")


class CustomerJson1(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str]

# Not Working
class CustomerJson2(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio")


class CustomerJson3(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio", default="")

Migrator().run()

# Hash
CustomerHash1(name="Brookins").save()
print(
    "HashModel with Optional and field: ",
    CustomerHash1.find(CustomerHash1.name == "Brookins").all(),
)
CustomerHash2(name="Brookins").save()
print(
    "HashModel with Optional and no default: ",
    CustomerHash2.find(CustomerHash2.name == "Brookins").all(),
)
CustomerHash3(name="Brookins").save()
print(
    "HashModel with Optional and default: ",
    CustomerHash3.find(CustomerHash3.name == "Brookins").all(),
)


# Json
CustomerJson1(name="Brookins").save()
print(
    "JsonModel with Optional and field: ",
    CustomerJson1.find(CustomerJson1.name == "Brookins").all(),
)
# Not Working
CustomerJson2(name="Brookins").save()
print(
    "JsonModel with Optional and no default: ",
    CustomerJson2.find(CustomerJson2.name == "Brookins").all(),
)
CustomerJson3(name="Brookins").save()
print(
    "JsonModel with Optional and default: ",
    CustomerJson3.find(CustomerJson3.name == "Brookins").all(),
)

Results:

HashModel with Optional and field:  [CustomerHash1(pk='01FR9YSQ61BKYERVVCZRYED6NG', name='Brookins', bio='')]
HashModel with Optional and no default:  [CustomerHash2(pk='01FR9YSQ64EXHRSA82HS9G9ZW6', name='Brookins', bio='')]
HashModel with Optional and default:  [CustomerHash3(pk='01FR9YSQ66FCNFB1GC55JR4NPZ', name='Brookins', bio='')]
JsonModel with Optional and field:  [CustomerJson1(pk='01FR9YSQ698MJTYY06KCTB3CQC', name='Brookins', bio=None)]
JsonModel with Optional and no default:  []
JsonModel with Optional and default:  [CustomerJson3(pk='01FR9YSQ6DXZZBY1BVMZWN59GC', name='Brookins', bio='')]

gam-phon avatar Jan 01 '22 04:01 gam-phon