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 in Pydantic and SQLAlchemy #30

Closed
8 tasks done
roomm opened this issue Aug 26, 2021 · 3 comments
Closed
8 tasks done

Add support for Decimal in Pydantic and SQLAlchemy #30

roomm opened this issue Aug 26, 2021 · 3 comments
Labels
answered feature New feature or request

Comments

@roomm
Copy link

roomm commented Aug 26, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class City(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)  # pylint: disable=invalid-name
    name: str = Field(nullable=False, max_length=150, index=False)
    population: Optional[int] = Field(default=None, index=False)
    latitude: condecimal(max_digits=13, decimal_places=10) = Field(nullable=False, index=False)
    longitude: condecimal(max_digits=13, decimal_places=10) = Field(nullable=False, index=False)

Description

When setting max_digits and decimal_places to condecimal these are ignored.
The SQL CREATE TABLE resulting from the previous model city definition results in:

CREATE TABLE city (
	id INTEGER AUTO_INCREMENT, 
	name VARCHAR(150) NOT NULL, 
	population INTEGER, 
	latitude NUMERIC NOT NULL, 
	longitude NUMERIC NOT NULL, 
	PRIMARY KEY (id)
)

So latitude and longitude are created with default parameters max_digits = 10 and decimal_places = 0.

Operating System

Linux

Operating System Details

Ubuntu 18.04

SQLModel Version

0.0.4

Python Version

3.7.11

Additional Context

This is my requirements.txt:

fastapi==0.68.1
uvicorn==0.15.0
python-multipart==0.0.5
pydantic==1.8.2
pymysql==1.0.2
python-jose[cryptography]
passlib
starlette-json
bcrypt
sqlmodel==0.0.4

@robcxyz
Copy link
Contributor

robcxyz commented Sep 19, 2021

I submitted a PR #103 to address this. Pydantic arguments needed to be mapped to sqlalchemy's for that type.

@tiangolo tiangolo added feature New feature or request and removed question Further information is requested labels Dec 13, 2021
@tiangolo tiangolo changed the title Set max_digits and decimal_places in condecimal are ignored Add support for Decimal in Pydantic and SQLAlchemy Dec 13, 2021
@tiangolo
Copy link
Owner

Thanks for the feedback @roomm! 🤓

This was added in @robcxyz's PR #103, it is now included in the (just released) SQLModel 0.0.5 🎉

@github-actions
Copy link

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants