Skip to content

Add support for Decimal in Pydantic and SQLAlchemy #30

@roomm

Description

@roomm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions