Skip to content
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

JsonModel: find not working with Optional and no default #58

Closed
gam-phon opened this issue Jan 1, 2022 · 1 comment
Closed

JsonModel: find not working with Optional and no default #58

gam-phon opened this issue Jan 1, 2022 · 1 comment

Comments

@gam-phon
Copy link

gam-phon commented Jan 1, 2022

Hello,

In JsonModel, find not working with class that has Optional and no default value, whereas HashModel is working

Check out below code:

class CustomerHash1(HashModel):
    name: str = Field(index=True)
    bio: Optional[str]


class CustomerHash2(HashModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio")


class CustomerHash3(HashModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio", default="")


class CustomerJson1(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str]

# Not Working
class CustomerJson2(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio")


class CustomerJson3(JsonModel):
    name: str = Field(index=True)
    bio: Optional[str] = Field(title="bio", default="")

Migrator().run()

# Hash
CustomerHash1(name="Brookins").save()
print(
    "HashModel with Optional and field: ",
    CustomerHash1.find(CustomerHash1.name == "Brookins").all(),
)
CustomerHash2(name="Brookins").save()
print(
    "HashModel with Optional and no default: ",
    CustomerHash2.find(CustomerHash2.name == "Brookins").all(),
)
CustomerHash3(name="Brookins").save()
print(
    "HashModel with Optional and default: ",
    CustomerHash3.find(CustomerHash3.name == "Brookins").all(),
)


# Json
CustomerJson1(name="Brookins").save()
print(
    "JsonModel with Optional and field: ",
    CustomerJson1.find(CustomerJson1.name == "Brookins").all(),
)
# Not Working
CustomerJson2(name="Brookins").save()
print(
    "JsonModel with Optional and no default: ",
    CustomerJson2.find(CustomerJson2.name == "Brookins").all(),
)
CustomerJson3(name="Brookins").save()
print(
    "JsonModel with Optional and default: ",
    CustomerJson3.find(CustomerJson3.name == "Brookins").all(),
)

Results:

HashModel with Optional and field:  [CustomerHash1(pk='01FR9YSQ61BKYERVVCZRYED6NG', name='Brookins', bio='')]
HashModel with Optional and no default:  [CustomerHash2(pk='01FR9YSQ64EXHRSA82HS9G9ZW6', name='Brookins', bio='')]
HashModel with Optional and default:  [CustomerHash3(pk='01FR9YSQ66FCNFB1GC55JR4NPZ', name='Brookins', bio='')]
JsonModel with Optional and field:  [CustomerJson1(pk='01FR9YSQ698MJTYY06KCTB3CQC', name='Brookins', bio=None)]
JsonModel with Optional and no default:  []
JsonModel with Optional and default:  [CustomerJson3(pk='01FR9YSQ6DXZZBY1BVMZWN59GC', name='Brookins', bio='')]
@gam-phon gam-phon changed the title find not working with Optional and no default JsonModel find not working with Optional and no default Jan 1, 2022
@gam-phon gam-phon changed the title JsonModel find not working with Optional and no default JsonModel: find not working with Optional and no default Jan 1, 2022
@sav-norem
Copy link

sav-norem commented Aug 29, 2022

This has since been fixed -

HashModel with Optional and field:  [CustomerHash1(pk='01GBN3QJMXME8WMG9C3MVXPT53', name='Brookins', bio='')]

HashModel with Optional and no default:  [CustomerHash2(pk='01GBN3QJN1VPGRCZZ1WHPC71C2', name='Brookins', bio='')]

HashModel with Optional and default:  [CustomerHash3(pk='01GBN3QJN3F3HCQ69N4R5EYKEB', name='Brookins', bio='')]

JsonModel with Optional and field:  [CustomerJson1(pk='01GBN3QJN5H7G8WAQ4J471CMWV', name='Brookins', bio=None)]

JsonModel with Optional and no default:  [CustomerJson2(pk='01GBN3QJN9YKYYC2KD5DB3P62J', name='Brookins', bio=None)]

JsonModel with Optional and default:  [CustomerJson3(pk='01GBN3QJNB72WAAQXZN95W6SPS', name='Brookins', bio='')]

I believe this can be closed @simonprickett

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

No branches or pull requests

3 participants