-
Notifications
You must be signed in to change notification settings - Fork 27
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
Correctly add default values to created pydantic models #68
base: master
Are you sure you want to change the base?
Conversation
This is a good idea. The only potential issue is sometimes the default is a callable. I can see that Pydantic's Perhaps this would work? params: t.Dict[str, t.Any] = {
"default": None if is_optional else ...,
"default_factory": column.get_default_value,
"nullable": column._meta.null,
} I need to dig into Pydantic some more to work out what the right approach would be. |
Hey, that definitely makes sense. Right now I'm on a vacation but definitely gonna look into this when I get back home. |
@kucera-lukas Cool, no rush - enjoy your vacation! |
@dantownsend Hi, I'm not sure if we can make this work because as I've discovered, every piccolo Also, |
@kucera-lukas Thanks for looking into this. You're right, each column always has a default. There's another attribute called class MyTable(Table):
name = Varchar(required=True) Piccolo itself does nothing with this attribute - it's just used to indicate to other tooling that the user should provide this value (Piccolo admin uses it for example). Is this a potential solution? So we only set the default attribute on the Pydantic model if |
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
Thanks a lot for this - will review and merge asap. |
"default": None if is_optional else ..., | ||
"nullable": column._meta.null, | ||
} | ||
if not column._meta.required: | ||
params.update({"default_factory": column.get_default_value}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's taking me a while to get this merged in - I just need to make sure this still works with the Piccolo admin.
In the old code we're setting the default to None
if is_optional
.
Right now default values are not being added (it's either
ellipsis
orNone
if we pass inall_optional
asTrue
). This is an issue because if people depend on the createdpydantic
schemas in their api endpoints the default values will not be parsed and will not be shown inopenapi
spec (eg.pydantic
andfastapi
schema generation).