Skip to content

Commit 19c14b2

Browse files
authored
Merge branch 'main' into main
2 parents f0fffe9 + 1b99c31 commit 19c14b2

36 files changed

+411
-110
lines changed

.github/workflows/build-docs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ jobs:
5151
- name: Install Material for MkDocs Insiders
5252
if: github.event.pull_request.head.repo.fork == false && steps.cache.outputs.cache-hit != 'true'
5353
run: python -m poetry run pip install git+https://${{ secrets.ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git
54+
- uses: actions/cache@v2
55+
with:
56+
key: mkdocs-cards-${{ github.ref }}
57+
path: .cache
5458
- name: Build Docs
59+
if: github.event.pull_request.head.repo.fork == true
5560
run: python -m poetry run mkdocs build
61+
- name: Build Docs with Insiders
62+
if: github.event.pull_request.head.repo.fork == false
63+
run: python -m poetry run mkdocs build --config-file mkdocs.insiders.yml
5664
- name: Zip docs
5765
run: python -m poetry run bash ./scripts/zip-docs.sh
5866
- uses: actions/upload-artifact@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ htmlcov
1111
coverage.xml
1212
site
1313
*.db
14+
.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,4 @@ And at the same time, ✨ it is also a **Pydantic** model ✨. You can use inher
212212

213213
## License
214214

215-
This project is licensed under the terms of the MIT license.
215+
This project is licensed under the terms of the [MIT license](https://github.com/tiangolo/sqlmodel/blob/main/LICENSE).

docs/advanced/decimal.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Decimal Numbers
2+
3+
In some cases you might need to be able to store decimal numbers with guarantees about the precision.
4+
5+
This is particularly important if you are storing things like **currencies**, **prices**, **accounts**, and others, as you would want to know that you wouldn't have rounding errors.
6+
7+
As an example, if you open Python and sum `1.1` + `2.2` you would expect to see `3.3`, but you will actually get `3.3000000000000003`:
8+
9+
```Python
10+
>>> 1.1 + 2.2
11+
3.3000000000000003
12+
```
13+
14+
This is because of the way numbers are stored in "ones and zeros" (binary). But Python has a module and some types to have strict decimal values. You can read more about it in the official <a href="https://docs.python.org/3/library/decimal.html" class="external-link" target="_blank">Python docs for Decimal</a>.
15+
16+
Because databases store data in the same ways as computers (in binary), they would have the same types of issues. And because of that, they also have a special **decimal** type.
17+
18+
In most cases this would probably not be a problem, for example measuring views in a video, or the life bar in a videogame. But as you can imagine, this is particularly important when dealing with **money** and **finances**.
19+
20+
## Decimal Types
21+
22+
Pydantic has special support for `Decimal` types using the <a href="https://pydantic-docs.helpmanual.io/usage/types/#arguments-to-condecimal" class="external-link" target="_blank">`condecimal()` special function</a>.
23+
24+
!!! tip
25+
Pydantic 1.9, that will be released soon, has improved support for `Decimal` types, without needing to use the `condecimal()` function.
26+
27+
But meanwhile, you can already use this feature with `condecimal()` in **SQLModel** it as it's explained here.
28+
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+
31+
!!! info
32+
For the database, **SQLModel** will use <a href="https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.DECIMAL" class="external-link" target="_blank">SQLAlchemy's `DECIMAL` type</a>.
33+
34+
## Decimals in SQLModel
35+
36+
Let's say that each hero in the database will have an amount of money. We could make that field a `Decimal` type using the `condecimal()` function:
37+
38+
```{.python .annotate hl_lines="12" }
39+
{!./docs_src/advanced/decimal/tutorial001.py[ln:1-12]!}
40+
41+
# More code here later 👇
42+
```
43+
44+
<details>
45+
<summary>👀 Full file preview</summary>
46+
47+
```Python
48+
{!./docs_src/advanced/decimal/tutorial001.py!}
49+
```
50+
51+
</details>
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+
78+
## Create models with Decimals
79+
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).
81+
82+
```Python hl_lines="4-6"
83+
# Code above omitted 👆
84+
85+
{!./docs_src/advanced/decimal/tutorial001.py[ln:25-35]!}
86+
87+
# Code below omitted 👇
88+
```
89+
90+
<details>
91+
<summary>👀 Full file preview</summary>
92+
93+
```Python
94+
{!./docs_src/advanced/decimal/tutorial001.py!}
95+
```
96+
97+
</details>
98+
99+
## Select Decimal data
100+
101+
Then, when working with Decimal types, you can confirm that they indeed avoid those rounding errors from floats:
102+
103+
```Python hl_lines="15-16"
104+
# Code above omitted 👆
105+
106+
{!./docs_src/advanced/decimal/tutorial001.py[ln:38-51]!}
107+
108+
# Code below omitted 👇
109+
```
110+
111+
<details>
112+
<summary>👀 Full file preview</summary>
113+
114+
```Python
115+
{!./docs_src/advanced/decimal/tutorial001.py!}
116+
```
117+
118+
</details>
119+
120+
## Review the results
121+
122+
Now if you run this, instead of printing the unexpected number `3.3000000000000003`, it prints `3.300`:
123+
124+
<div class="termy">
125+
126+
```console
127+
$ python app.py
128+
129+
// Some boilerplate and previous output omitted 😉
130+
131+
// The type of money is Decimal('1.100')
132+
Hero 1: id=1 secret_name='Dive Wilson' age=None name='Deadpond' money=Decimal('1.100')
133+
134+
// More output omitted here 🤓
135+
136+
// The type of money is Decimal('1.100')
137+
Hero 2: id=3 secret_name='Tommy Sharp' age=48 name='Rusty-Man' money=Decimal('2.200')
138+
139+
// No rounding errors, just 3.3! 🎉
140+
Total money: 3.300
141+
```
142+
143+
</div>
144+
145+
!!! warning
146+
Although Decimal types are supported and used in the Python side, not all databases support it. In particular, SQLite doesn't support decimals, so it will convert them to the same floating `NUMERIC` type it supports.
147+
148+
But decimals are supported by most of the other SQL databases. 🎉

docs/advanced/index.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Advanced User Guide
22

3-
The **Advanced User Guide** will be coming soon to a <del>theater</del> **documentation** near you... 😅
3+
The **Advanced User Guide** is gradually growing, you can already read about some advanced topics.
44

5-
I just have to `add` it, `commit` it, and `refresh` it. 😉
5+
At some point it will include:
66

7-
It will include:
8-
9-
* How to use the `async` and `await` with the async session.
7+
* How to use `async` and `await` with the async session.
108
* How to run migrations.
119
* How to combine **SQLModel** models with SQLAlchemy.
12-
* ...and more.
10+
* ...and more. 🤓

docs/databases.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Some examples of databases that work like this could be **PostgreSQL**, **MySQL*
8585

8686
### Distributed servers
8787

88-
In some cases, the database could even be a group server applications running on different machines, working together and communicating between them to be more efficient and handle more data.
88+
In some cases, the database could even be a group of server applications running on different machines, working together and communicating between them to be more efficient and handle more data.
8989

9090
In this case, your code would talk to one or more of these server applications running on different machines.
9191

@@ -250,7 +250,7 @@ As these **primary key** IDs can uniquely identify each row on the table for tea
250250

251251
<img alt="table relationships" src="/img/databases/relationships.svg">
252252

253-
So, in the table for heroes, we use the `team_id` column to define a relationship to the *foreign* table for teams. Each value in the `team_id` column on the table with heroes will be the same value as the `id` column of one row in the table wiwth teams.
253+
So, in the table for heroes, we use the `team_id` column to define a relationship to the *foreign* table for teams. Each value in the `team_id` column on the table with heroes will be the same value as the `id` column of one row in the table with teams.
254254

255255
In the table for heroes we have a **primary key** that is the `id`. But we also have another column `team_id` that refers to a **key** in a **foreign** table. There's a technical term for that too, the `team_id` is a "**foreign key**".
256256

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,4 @@ And at the same time, ✨ it is also a **Pydantic** model ✨. You can use inher
212212

213213
## License
214214

215-
This project is licensed under the terms of the MIT license.
215+
This project is licensed under the terms of the [MIT license](https://github.com/tiangolo/sqlmodel/blob/main/LICENSE).

docs/release-notes.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
## Latest Changes
44

5+
* 📝 Add links to the license file. PR [#29](https://github.com/tiangolo/sqlmodel/pull/29) by [@sobolevn](https://github.com/sobolevn).
6+
* ✏ Fix typos in docs titles. PR [#28](https://github.com/tiangolo/sqlmodel/pull/28) by [@Batalex](https://github.com/Batalex).
7+
* ✏ Fix multiple typos and some rewording. PR [#22](https://github.com/tiangolo/sqlmodel/pull/22) by [@egrim](https://github.com/egrim).
8+
* ✏ Fix typo in `docs/tutorial/automatic-id-none-refresh.md`. PR [#14](https://github.com/tiangolo/sqlmodel/pull/14) by [@leynier](https://github.com/leynier).
9+
* ✏ Fix typos in `docs/tutorial/index.md` and `docs/databases.md`. PR [#5](https://github.com/tiangolo/sqlmodel/pull/5) by [@sebastianmarines](https://github.com/sebastianmarines).
10+
11+
## 0.0.5
12+
13+
### Features
14+
15+
* ✨ Add support for Decimal fields from Pydantic and SQLAlchemy. Original PR [#103](https://github.com/tiangolo/sqlmodel/pull/103) by [@robcxyz](https://github.com/robcxyz). New docs: [Advanced User Guide - Decimal Numbers](https://sqlmodel.tiangolo.com/advanced/decimal/).
16+
17+
### Docs
18+
19+
* ✏ Update decimal tutorial source for consistency. PR [#188](https://github.com/tiangolo/sqlmodel/pull/188) by [@tiangolo](https://github.com/tiangolo).
20+
21+
### Internal
22+
23+
* 🔧 Split MkDocs insiders build in CI to support building from PRs. PR [#186](https://github.com/tiangolo/sqlmodel/pull/186) by [@tiangolo](https://github.com/tiangolo).
24+
* 🎨 Format `expression.py` and expression template, currently needed by CI. PR [#187](https://github.com/tiangolo/sqlmodel/pull/187) by [@tiangolo](https://github.com/tiangolo).
25+
* 🐛Fix docs light/dark theme switcher. PR [#1](https://github.com/tiangolo/sqlmodel/pull/1) by [@Lehoczky](https://github.com/Lehoczky).
26+
* 🔧 Add MkDocs Material social cards. PR [#90](https://github.com/tiangolo/sqlmodel/pull/90) by [@tiangolo](https://github.com/tiangolo).
27+
* ✨ Update type annotations and upgrade mypy. PR [#173](https://github.com/tiangolo/sqlmodel/pull/173) by [@tiangolo](https://github.com/tiangolo).
528

629
## 0.0.4
730

docs/tutorial/automatic-id-none-refresh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ In this case, after committing the object to the database with the **session**,
399399

400400
## Print Data After Closing the Session
401401

402-
Now, as a fnal experiment, we can also print data after the **session** is closed.
402+
Now, as a final experiment, we can also print data after the **session** is closed.
403403

404404
There are no surprises here, it still works:
405405

docs/tutorial/connect/create-connected-tables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Most of that should look familiar:
107107

108108
The column will be named `team_id`. It will be an integer, and it could be `NULL` in the database (or `None` in Python), becase there could be some heroes that don't belong to any team.
109109

110-
As we don't have to explicitly pass `team_id=None` when creating a hero, we add a default of `None` to the `Field()`.
110+
We add a default of `None` to the `Field()` so we don't have to explicitly pass `team_id=None` when creating a hero.
111111

112112
Now, here's the new part:
113113

0 commit comments

Comments
 (0)