Thanks to visit codestin.com
Credit goes to github.com

Skip to content

optional property for Model cannot be None #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wasperen2 opened this issue Jul 2, 2024 · 0 comments · Fixed by #40
Closed

optional property for Model cannot be None #37

wasperen2 opened this issue Jul 2, 2024 · 0 comments · Fixed by #40

Comments

@wasperen2
Copy link

wasperen2 commented Jul 2, 2024

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.

@wasperen2 wasperen2 changed the title optional property cannot be None optional property for Model cannot be None Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant