FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

✨ Add support for Decimal fields from Pydantic and SQLAlchemy by robcxyz · Pull Request #103 · fastapi/sqlmodel · GitHub

Repository navigation

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

Merged
tiangolo merged 12 commits into
fastapi:mainfrom
robcxyz:fix_decimal_attributes
Dec 13, 2021
Merged

tiangolo merged 12 commits into
fastapi:mainfrom
robcxyz:fix_decimal_attributes

Conversation

robcxyz commented Sep 19, 2021 •
edited
Loading

Copy link
Copy Markdown
Contributor

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 changed the title Fix decimal attributes ✨ Add support for Decimal fields from Pydantic and SQLAlchemy Dec 13, 2021

codecov Bot commented Dec 13, 2021 •
edited
Loading

Copy link
Copy Markdown

Codecov Report

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

@@            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.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

tiangolo merged commit 580f372 into fastapi:main Dec 13, 2021

Copy link
Copy Markdown
Member

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 🚀 🎉

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

2 participants


Back | FazBrowse Home | New Git URL