sopherapps / pydantic-redis

A simple Declarative ORM for Redis using pydantic Models
https://sopherapps.github.io/pydantic-redis
MIT License
43 stars 14 forks source link

Can't work with models that contain tuple of other models #12

Closed Tinitto closed 1 year ago

Tinitto commented 1 year ago

Currently a model like the ScoreBoard model below with the field scores being a tuple of string and Model fails to work

from typing import Tuple
from pydantic_redis import RedisConfig, Model, Store

class Score(Model):
    _primary_key_field: str = 'id'
    id: str
    total: int

class ScoreBoard(Model):
    _primary_key_field: str = 'id'
    id: str
    scores: Tuple[str, Score]

if __name__ == '__main__':
    store = Store(name="test", redis_config=RedisConfig())
    store.redis_store.flushall()
    store.register_model(Score)
    store.register_model(ScoreBoard)

    example_score = Score(id="some id", total=50)

    ScoreBoard.insert(data=ScoreBoard(id='test', scores=("mark", example_score,)))
    items = ScoreBoard.select(ids=["test"])
    print(f"{items}")

It raises the following exception:

Traceback (most recent call last):
  File "pydantic_nested_tuples.py", line 27, in <module>
    items = ScoreBoard.select(ids=["test"])
  File "/Users/apple.inc/work/code/sopherapps/pydantic-redis/pydantic_redis/model.py", line 139, in select
    return cls.__parse_model_list(response)
  File "/Users/apple.inc/work/code/sopherapps/pydantic-redis/pydantic_redis/model.py", line 237, in __parse_model_list
    return [cls(**record) for record in parsed_dict_list]
  File "/Users/apple.inc/work/code/sopherapps/pydantic-redis/pydantic_redis/model.py", line 237, in <listcomp>
    return [cls(**record) for record in parsed_dict_list]
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ScoreBoard
scores -> 1
  value is not a valid dict (type=type_error.dict)

Pydantic_redis version: 0.1.6