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

Optional[int] raise error if missing

Open dusking opened this issue 2 years ago • 2 comments

If I have a model with the following field: number: Optional[int]

I can create a record without the number. But, when I'll call model.get(pk) - I'll get an error: value is not a valid integer (type=type_error.integer)

dusking avatar May 23 '22 15:05 dusking

Just to be clear, this issue exist for other data types as well (like datetime, float, etc).

Here sample code to replicate this issue:

import datetime
from datetime import datetime
from typing import Optional

from redis_om import Field, HashModel, Migrator, get_redis_connection

# This Redis instance is tuned for durability.
REDIS_DATA_URL = "redis://localhost:6379"

class Person(HashModel):
    first_name: str = Field(index=True)
    last_name: str = Field(index=True)
    emp_no: int =  Field(index=True, sortable=True)
    lastDate: Optional[datetime]
    dailyWage: Optional[float]
    workDayPerWeek: Optional[int]

# set redis connection
Person.Meta.database = get_redis_connection(url=REDIS_DATA_URL,
                                                  decode_responses=True)
# apply migrations
Migrator().run()


# add employe
emp_no = 999
person = Person(first_name="John" + str(emp_no), last_name="Doe", emp_no=emp_no)
person.save()
    
# get employee
result = Person.find().all()

msarm avatar Jun 18 '22 11:06 msarm

This is being worked on - but in the meantime if you can, this issue only applies to HashModels and you can swap it for a JsonModel and it will work

sav-norem avatar Sep 09 '22 16:09 sav-norem