sopherapps / pydantic-redis

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

optional property for Model cannot be None #37

Closed wasperen2 closed 1 month ago

wasperen2 commented 2 months ago

Describe the bug If the value of an optional property is None, storing a Model fails

To Reproduce Running the following code:

import uuid
from typing import Optional

from pydantic import Field
from pydantic_redis import Store, RedisConfig, Model

class Child(Model):
    _primary_key_field = "id"

    id: str = Field(
        default_factory=lambda: uuid.uuid4().hex,
    )
    name: str = Field()

class Parent(Model):
    _primary_key_field = "id"
    id: str = Field(
        default_factory=lambda: uuid.uuid4().hex,
    )
    name: str = Field()
    child: Optional[Child] = Field(default=None)

store = Store(
    name="does_none_work",
    redis_config=RedisConfig(host='localhost', port=6379, db=6)
)

store.register_model(Child)
store.register_model(Parent)

parent = Parent(name="bong bing")

Parent.insert(parent)

parents_retrieved = Parent.select(ids=[parent.id])
print(parents_retrieved)

Fails with the following exception:

AttributeError: type object 'NoneType' has no attribute 'get_primary_key_field'

Expected behavior One would expect the Parent object to be just stored into Redis with the Optional property left to None.