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

Error while having multiple levels of Embedded Json Model

Open Mograbi opened this issue 2 years ago • 1 comments

I'm trying out the following example:

from redis_om import Migrator, Field, JsonModel, get_redis_connection, EmbeddedJsonModel, HashModel
from pydantic import BaseModel
from typing import Optional


class Level2(EmbeddedJsonModel):
    c: str


class Level1(EmbeddedJsonModel):
    a: Optional[Level2]


class Model(JsonModel):
    a: str = Field(default="a", index=True)
    b: str = Field(index=True)
    c: Level1

    class Meta:
        global_key_prefix = "Mine"
        database = get_redis_connection(url="redis://172.17.0.2", db=0)
        model_key_prefix = "Model"


Migrator().run()
a1 = Model(**{"a": "a1", "b": "b1", "c": {"a": {"c": "empty"}}})

but keep getting the following error while running:

  File "/home/mmograbi/github/redis-om/test.py", line 26, in <module>
    a1 = Model(**{"a": "a1", "b": "b1", "c": {"a": {"c": "empty"}}})
  File "/home/mmograbi/.local/lib/python3.8/site-packages/redis_om/model/model.py", line 1510, in __init__
    super().__init__(*args, **kwargs)
  File "/home/mmograbi/.local/lib/python3.8/site-packages/redis_om/model/model.py", line 1131, in __init__
    super().__init__(**data)
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Model
c
  'NoneType' object is not subscriptable (type=type_error)

if I replace the EmbeddedJsonModel with BaseModel from pydantic it works as expected without exception.

Env:

  • Python 3.8.10
  • pydantic 1.10.6
  • redis-om 0.1.2
  • ubuntu 20.04

Mograbi avatar Mar 13 '23 18:03 Mograbi

@slorello89 Not sure if this is being looked into.

I'm thinking of having indexes on some of my embedded models. Would like to know if things are stable.

XChikuX avatar Dec 09 '24 08:12 XChikuX