redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.07k stars 108 forks source link

How does one find a model with a None field value? #485

Open YaraslauZhylko opened 1 year ago

YaraslauZhylko commented 1 year ago

I have a model like this:

class City(JsonModel):
    country_id: str = Field(index=True)
    region_id: str | None = Field(index=True)

How do I query for a city in a specific country that is not associated with any region? I.e.: a capital or a large city like Bremen in Germany?

The following queries do not work and raise errors:

cities = City.find((City.country_id == "de") & (City.region_id is None)).all()

aioredis_om.model.model.QuerySyntaxError: Counld not resolve field name. See docs: TODO
cities = City.find((City.country_id == "de") & (City.region_id == None)).all()

TypeError: argument of type 'NoneType' is not iterable

I failed to find any reference in the docs about scenarios like that... 🤔

P.S.: SQLAlchemy has .filter(City.region_id == None) or .filter(City.region_id.is_not(None)) to handle those cases.