You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced/decimal.md
+26-1Lines changed: 26 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Pydantic has special support for `Decimal` types using the <a href="https://pyda
26
26
27
27
But meanwhile, you can already use this feature with `condecimal()` in **SQLModel** it as it's explained here.
28
28
29
-
When you use `condecimal()` you can specify the number of decimal places and digits to support. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
29
+
When you use `condecimal()` you can specify the number of digits and decimal places to support. They will be validated by Pydantic (for example when using FastAPI) and the same information will also be used for the database columns.
30
30
31
31
!!! info
32
32
For the database, **SQLModel** will use <ahref="https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.DECIMAL"class="external-link"target="_blank">SQLAlchemy's `DECIMAL` type</a>.
@@ -50,6 +50,31 @@ Let's say that each hero in the database will have an amount of money. We could
50
50
51
51
</details>
52
52
53
+
Here we are saying that `money` can have at most `5` digits with `max_digits`, **this includes the integers** (to the left of the decimal dot) **and the decimals** (to the right of the decimal dot).
54
+
55
+
We are also saying that the number of decimal places (to the right of the decimal dot) is `3`, so we can have **3 decimal digits** for these numbers in the `money` field. This means that we will have **2 digits for the integer part** and **3 digits for the decimal part**.
56
+
57
+
✅ So, for example, these are all valid numbers for the `money` field:
58
+
59
+
*`12.345`
60
+
*`12.3`
61
+
*`12`
62
+
*`1.2`
63
+
*`0.123`
64
+
*`0`
65
+
66
+
🚫 But these are all invalid numbers for that `money` field:
67
+
68
+
*`1.2345`
69
+
* This number has more than 3 decimal places.
70
+
*`123.234`
71
+
* This number has more than 5 digits in total (integer and decimal part).
72
+
*`123`
73
+
* Even though this number doesn't have any decimals, we still have 3 places saved for them, which means that we can **only use 2 places** for the **integer part**, and this number has 3 integer digits. So, the allowed number of integer digits is `max_digits` - `decimal_places` = 2.
74
+
75
+
!!! tip
76
+
Make sure you adjust the number of digits and decimal places for your own needs, in your own application. 🤓
77
+
53
78
## Create models with Decimals
54
79
55
80
When creating new models you can actually pass normal (`float`) numbers, Pydantic will automatically convert them to `Decimal` types, and **SQLModel** will store them as `Decimal` types in the database (using SQLAlchemy).
0 commit comments