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

✨ Add support for Decimal fields from Pydantic and SQLAlchemy #103

Merged
merged 12 commits into from Dec 13, 2021

Conversation

robcxyz
Copy link
Contributor

@robcxyz robcxyz commented Sep 19, 2021

Per issue #30 - condecimal attributes aren't passed down into sqlalchemy types. This PR fixes that by mapping pydantic's condecimal arguments max_digits and decimal_places to sqlalchemy's decimal arguments precision and scale respectively.

main.py #395

    if issubclass(field.type_, Decimal):
        return Numeric(
            precision=getattr(field.type_, "max_digits", None),
            scale=getattr(field.type_, "decimal_places", None),
        )

Also adds an example with a test in the code_structure tutorial with a condecimal field with a few options.

@tiangolo tiangolo changed the title Fix decimal attributes ✨ Add support for Decimal fields from Pydantic and SQLAlchemy Dec 13, 2021
@codecov
Copy link

codecov bot commented Dec 13, 2021

Codecov Report

Merging #103 (0a83e2b) into main (14a9788) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #103      +/-   ##
==========================================
+ Coverage   97.27%   97.43%   +0.16%     
==========================================
  Files         175      177       +2     
  Lines        5842     5895      +53     
==========================================
+ Hits         5683     5744      +61     
+ Misses        159      151       -8     
Impacted Files Coverage Δ
docs_src/advanced/decimal/tutorial001.py 100.00% <100.00%> (ø)
sqlmodel/main.py 81.73% <100.00%> (+2.39%) ⬆️
...sts/test_advanced/test_decimal/test_tutorial001.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14a9788...0a83e2b. Read the comment docs.

@github-actions
Copy link

📝 Docs preview for commit b3097dd at: https://61b7250eedfd1107eee4102a--sqlmodel.netlify.app

@github-actions
Copy link

📝 Docs preview for commit f3ee8b9 at: https://61b72675216a992d57cdf825--sqlmodel.netlify.app

@github-actions
Copy link

📝 Docs preview for commit 0a83e2b at: https://61b72e60030c1ad58c750965--sqlmodel.netlify.app

@tiangolo tiangolo merged commit 580f372 into tiangolo:main Dec 13, 2021
@tiangolo
Copy link
Owner

Awesome! Thanks for the contribution and the support @robcxyz! 🚀 🎉

I updated the docs and tests, this now has its own section in the advanced docs explaining a bit how decimals work: https://sqlmodel.tiangolo.com/advanced/decimal/

Before updating and merging this I also added this PR to Pydantic, which means that in the next Pydantic version we'll be able to have types annotated with Decimal directly instead of condecimal(), keeping editor support, etc. pydantic/pydantic#3507 🎉

This is now included in the (just released) SQLModel 0.0.5 🚀 🎉

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 this pull request may close these issues.

None yet

2 participants