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/release-notes.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,9 @@
2
2
3
3
## Latest Changes
4
4
5
+
* ✏ Fix typo in `docs/tutorial/relationship-attributes/define-relationships-attributes.md`. PR [#239](https://github.com/tiangolo/sqlmodel/pull/239) by [@jalvaradosegura](https://github.com/jalvaradosegura).
6
+
* ✏ Fix typo in `docs/tutorial/fastapi/simple-hero-api.md`. PR [#80](https://github.com/tiangolo/sqlmodel/pull/80) by [@joemudryk](https://github.com/joemudryk).
7
+
* ✏ Fix typos in multiple files in the docs. PR [#400](https://github.com/tiangolo/sqlmodel/pull/400) by [@VictorGambarini](https://github.com/VictorGambarini).
5
8
* ✏ Fix typo in `docs/tutorial/code-structure.md`. PR [#344](https://github.com/tiangolo/sqlmodel/pull/344) by [@marciomazza](https://github.com/marciomazza).
6
9
* ✏ Fix typo in `docs/db-to-code.md`. PR [#155](https://github.com/tiangolo/sqlmodel/pull/155) by [@gr8jam](https://github.com/gr8jam).
7
10
* ✏ Fix typo in `docs/contributing.md`. PR [#323](https://github.com/tiangolo/sqlmodel/pull/323) by [@Fardad13](https://github.com/Fardad13).
Copy file name to clipboardExpand all lines: docs/tutorial/automatic-id-none-refresh.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Automatic IDs, None Defaults, and Refreshing Data
2
2
3
-
In the previous chapter we saw how to add rows to the database using **SQLModel**.
3
+
In the previous chapter, we saw how to add rows to the database using **SQLModel**.
4
4
5
5
Now let's talk a bit about why the `id` field **can't be `NULL`** on the database because it's a **primary key**, and we declare it using `Field(primary_key=True)`.
6
6
@@ -11,7 +11,7 @@ But the same `id` field actually **can be `None`** in the Python code, so we dec
@@ -68,7 +68,7 @@ If we ran this code before saving the hero to the database and the `hero_1.id` w
68
68
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
69
69
```
70
70
71
-
But by declaring it with `Optional[int]` the editor will help us to avoid writing broken code by showing us a warning telling us that the code could be invalid if `hero_1.id` is `None`. 🔍
71
+
But by declaring it with `Optional[int]`, the editor will help us to avoid writing broken code by showing us a warning telling us that the code could be invalid if `hero_1.id` is `None`. 🔍
72
72
73
73
## Print the Default `id` Values
74
74
@@ -79,7 +79,7 @@ We can confirm that by printing our heroes before adding them to the database:
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/limit-and-offset.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,14 @@
2
2
3
3
When a client sends a request to get all the heroes, we have been returning them all.
4
4
5
-
But if we had **thousands** of heroes that could consume a lot of **computational resources**, network bandwith, etc.
5
+
But if we had **thousands** of heroes that could consume a lot of **computational resources**, network bandwidth, etc.
6
6
7
-
So we probably want to limit it.
7
+
So, we probably want to limit it.
8
8
9
9
Let's use the same **offset** and **limit** we learned about in the previous tutorial chapters for the API.
10
10
11
11
!!! info
12
-
In many cases this is also called **pagination**.
12
+
In many cases, this is also called **pagination**.
13
13
14
14
## Add a Limit and Offset to the Query Parameters
15
15
@@ -38,13 +38,13 @@ And by default, we will return a maximum of `100` heroes, so `limit` will have a
38
38
39
39
</details>
40
40
41
-
We want to allow clients to set a different `offset` and `limit` values.
41
+
We want to allow clients to set different `offset` and `limit` values.
42
42
43
43
But we don't want them to be able to set a `limit` of something like `9999`, that's over `9000`! 😱
44
44
45
45
So, to prevent it, we add additional validation to the `limit` query parameter, declaring that it has to be **l**ess **t**han or **e**qual to `100` with `lte=100`.
46
46
47
-
This way, a client can decide to take less heroes if they want, but not more.
47
+
This way, a client can decide to take fewer heroes if they want, but not more.
48
48
49
49
!!! info
50
50
If you need to refresh how query parameters and their validation work, check out the docs in FastAPI:
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/multiple-models.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
3
3
We have been using the same `Hero` model to declare the schema of the data we receive in the API, the table model in the database, and the schema of the data we send back in responses.
4
4
5
-
But in most of the cases there are slight differences, let's use multiple models to solve it.
5
+
But in most of the cases, there are slight differences. Let's use multiple models to solve it.
6
6
7
7
Here you will see the main and biggest feature of **SQLModel**. 😎
8
8
9
9
## Review Creation Schema
10
10
11
11
Let's start by reviewing the automatically generated schemas from the docs UI.
12
12
13
-
For input we have:
13
+
For input, we have:
14
14
15
15
<imgclass="shadow"alt="Interactive API docs UI"src="/img/tutorial/fastapi/simple-hero-api/image01.png">
16
16
@@ -20,7 +20,7 @@ This means that the client could try to use the same ID that already exists in t
20
20
21
21
That's not what we want.
22
22
23
-
We want the client to only send the data that is needed to create a new hero:
23
+
We want the client only to send the data that is needed to create a new hero:
24
24
25
25
*`name`
26
26
*`secret_name`
@@ -63,7 +63,7 @@ The ultimate goal of an API is for some **clients to use it**.
63
63
64
64
The clients could be a frontend application, a command line program, a graphical user interface, a mobile application, another backend application, etc.
65
65
66
-
And the code those clients write depend on what our API tells them they **need to send**, and what they can **expect to receive**.
66
+
And the code those clients write depends on what our API tells them they **need to send**, and what they can **expect to receive**.
67
67
68
68
Making both sides very clear will make it much easier to interact with the API.
69
69
@@ -164,7 +164,7 @@ Let's first check how is the process to create a hero now:
164
164
165
165
Let's check that in detail.
166
166
167
-
Now we use the type annotation `HeroCreate` for the request JSON data, in the `hero` parameter of the **path operation function**.
167
+
Now we use the type annotation `HeroCreate` for the request JSON data in the `hero` parameter of the **path operation function**.
168
168
169
169
```Python hl_lines="3"
170
170
# Code above omitted 👆
@@ -180,9 +180,9 @@ The method `.from_orm()` reads data from another object with attributes and crea
180
180
181
181
The alternative is `Hero.parse_obj()` that reads data from a dictionary.
182
182
183
-
But as in this case we have a `HeroCreate` instance in the `hero` variable, this is an object with attributes, so we use `.from_orm()` to read those attributes.
183
+
But as in this case, we have a `HeroCreate` instance in the `hero` variable. This is an object with attributes, so we use `.from_orm()` to read those attributes.
184
184
185
-
With this we create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request.
185
+
With this, we create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request.
186
186
187
187
```Python hl_lines="3"
188
188
# Code above omitted 👆
@@ -192,7 +192,7 @@ With this we create a new `Hero` instance (the one for the database) and put it
192
192
# Code below omitted 👇
193
193
```
194
194
195
-
Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally we return the same `db_hero` variable that has the just refreshed `Hero` instance.
195
+
Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance.
196
196
197
197
Because it is just refreshed, it has the `id` field set with a new ID taken from the database.
198
198
@@ -206,30 +206,30 @@ And now that we return it, FastAPI will validate the data with the `response_mod
206
206
# Code below omitted 👇
207
207
```
208
208
209
-
This will validate that all the data that we promised is there, and will remove any data we didn't declare.
209
+
This will validate that all the data that we promised is there and will remove any data we didn't declare.
210
210
211
211
!!! tip
212
-
This filtering could be very important, and could be a very good security feature, for example to make sure you filter private data, hashed passwords, etc.
212
+
This filtering could be very important and could be a very good security feature, for example, to make sure you filter private data, hashed passwords, etc.
213
213
214
214
You can read more about it in the <a href="https://fastapi.tiangolo.com/tutorial/response-model/" class="external-link" target="_blank">FastAPI docs about Response Model</a>.
215
215
216
-
In particular, it will make sure that the `id` is there, and that it is indeed an integer (and not `None`).
216
+
In particular, it will make sure that the `id` is there and that it is indeed an integer (and not `None`).
217
217
218
218
## Shared Fields
219
219
220
220
But looking closely, we could see that these models have a lot of **duplicated information**.
221
221
222
-
All **the 3 models** declare that thay share some **common fields** that look exactly the same:
222
+
All **the 3 models** declare that they share some **common fields** that look exactly the same:
223
223
224
224
*`name`, required
225
225
*`secret_name`, required
226
226
*`age`, optional
227
227
228
-
And then they declare other fields with some differences (in this case only about the `id`).
228
+
And then they declare other fields with some differences (in this case, only about the `id`).
229
229
230
230
We want to **avoid duplicated information** if possible.
231
231
232
-
This is important if, for example, in the future we decide to **refactor the code** and rename one field (column). For example, from `secret_name` to `secret_identity`.
232
+
This is important if, for example, in the future, we decide to **refactor the code** and rename one field (column). For example, from `secret_name` to `secret_identity`.
233
233
234
234
If we have that duplicated in multiple models, we could easily forget to update one of them. But if we **avoid duplication**, there's only one place that would need updating. ✨
235
235
@@ -363,7 +363,7 @@ This means that there's nothing else special in this class apart from the fact t
363
363
364
364
As an alternative, we could use `HeroBase` directly in the API code instead of `HeroCreate`, but it would show up in the automatic docs UI with that name "`HeroBase`" which could be **confusing** for clients. Instead, "`HeroCreate`" is a bit more explicit about what it is for.
365
365
366
-
On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example a password), and now we already have the class to put those extra fields.
366
+
On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example, a password), and now we already have the class to put those extra fields.
367
367
368
368
### The `HeroRead`**Data Model**
369
369
@@ -390,7 +390,7 @@ This one just declares that the `id` field is required when reading a hero from
390
390
391
391
## Review the Updated Docs UI
392
392
393
-
The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroRead`. But now we define them in a smarter way with inheritance.
393
+
The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroRead`. But now, we define them in a smarter way with inheritance.
394
394
395
395
So, we can jump to the docs UI right away and see how they look with the updated data.
396
396
@@ -400,7 +400,7 @@ Let's see the new UI for creating a hero:
400
400
401
401
<imgclass="shadow"alt="Interactive API docs UI"src="/img/tutorial/fastapi/multiple-models/image02.png">
402
402
403
-
Nice! It now shows that to create a hero, we just pass the `name`, `secret_name`, and optinally`age`.
403
+
Nice! It now shows that to create a hero, we just pass the `name`, `secret_name`, and optionally`age`.
404
404
405
405
We no longer pass an `id`.
406
406
@@ -416,7 +416,7 @@ And if we check the schema for the **Read Heroes** *path operation* it will also
416
416
417
417
## Inheritance and Table Models
418
418
419
-
We just saw how powerful inheritance of these models can be.
419
+
We just saw how powerful the inheritance of these models could be.
420
420
421
421
This is a very simple example, and it might look a bit... meh. 😅
Copy file name to clipboardExpand all lines: docs/tutorial/fastapi/relationships.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,7 +102,7 @@ In this case, we used `response_model=TeamRead` and `response_model=HeroRead`, s
102
102
103
103
Now let's stop for a second and think about it.
104
104
105
-
We cannot simply include *all* the data including all the internal relationships, because each **hero** has an attribute `team` with their team, and then that **team** also has an attribute `heroes` with all the **heroes** in the team, including this one.
105
+
We cannot simply include *all* the data, including all the internal relationships, because each **hero** has an attribute `team` with their team, and then that **team** also has an attribute `heroes` with all the **heroes** in the team, including this one.
106
106
107
107
If we tried to include everything, we could make the server application **crash** trying to extract **infinite data**, going through the same hero and team over and over again internally, something like this:
108
108
@@ -152,7 +152,7 @@ If we tried to include everything, we could make the server application **crash*
152
152
}
153
153
```
154
154
155
-
As you can see, in this example we would get the hero **Rusty-Man**, and from this hero we would get the team **Preventers**, and then from this team we would get its heroes, of course, including **Rusty-Man**... 😱
155
+
As you can see, in this example, we would get the hero **Rusty-Man**, and from this hero we would get the team **Preventers**, and then from this team we would get its heroes, of course, including **Rusty-Man**... 😱
156
156
157
157
So we start again, and in the end, the server would just crash trying to get all the data with a `"Maximum recursion error"`, we would not even get a response like the one above.
158
158
@@ -164,7 +164,7 @@ This is a decision that will depend on **each application**.
164
164
165
165
In our case, let's say that if we get a **list of heroes**, we don't want to also include each of their teams in each one.
166
166
167
-
And if we get a **list of teams**, we don't want to get a a list of the heroes for each one.
167
+
And if we get a **list of teams**, we don't want to get a list of the heroes for each one.
168
168
169
169
But if we get a **single hero**, we want to include the team data (without the team's heroes).
170
170
@@ -195,15 +195,15 @@ We'll add them **after** the other models so that we can easily reference the pr
195
195
196
196
</details>
197
197
198
-
These two models are very **simple in code**, but there's a lot happening here, let's check it out.
198
+
These two models are very **simple in code**, but there's a lot happening here. Let's check it out.
199
199
200
200
### Inheritance and Type Annotations
201
201
202
202
The `HeroReadWithTeam`**inherits** from `HeroRead`, which means that it will have the **normal fields for reading**, including the required `id` that was declared in `HeroRead`.
203
203
204
204
And then it adds the **new field**`team`, which could be `None`, and is declared with the type `TeamRead` with the base fields for reading a team.
205
205
206
-
Then we do the same for the `TeamReadWithHeroes`, it **inherits** from `TeamRead`, and declare the **new field**`heroes` which is a list of `HeroRead`.
206
+
Then we do the same for the `TeamReadWithHeroes`, it **inherits** from `TeamRead`, and declares the **new field**`heroes`, which is a list of `HeroRead`.
207
207
208
208
### Data Models Without Relationship Attributes
209
209
@@ -213,7 +213,7 @@ Instead, here these are only **data models** that will tell FastAPI **which attr
213
213
214
214
### Reference to Other Models
215
215
216
-
Also notice that the field `team` is not declared with this new `TeamReadWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamRead` model.
216
+
Also, notice that the field `team` is not declared with this new `TeamReadWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamRead` model.
217
217
218
218
And the same for `TeamReadWithHeroes`, the model used for the new field `heroes` uses `HeroRead` to get only each hero's data.
219
219
@@ -326,12 +326,12 @@ Now we get the list of **heroes** included:
326
326
327
327
## Recap
328
328
329
-
Using the same techniques to declare additonal**data models** we can tell FastAPI what data to return in the responses, even when we return **table models**.
329
+
Using the same techniques to declare additional**data models**, we can tell FastAPI what data to return in the responses, even when we return **table models**.
330
330
331
331
Here we almost **didn't have to change the FastAPI app** code, but of course, there will be cases where you need to get the data and process it in different ways in the *path operation function* before returning it.
332
332
333
333
But even in those cases, you will be able to define the **data models** to use in `response_model` to tell FastAPI how to validate and filter the data.
334
334
335
335
By this point, you already have a very robust API to handle data in a SQL database combining **SQLModel** with **FastAPI**, and implementing **best practices**, like data validation, conversion, filtering, and documentation. ✨
336
336
337
-
In the next chapter I'll tell you how to implement automated **testing** for your application using FastAPI and SQLModel. ✅
337
+
In the next chapter, I'll tell you how to implement automated **testing** for your application using FastAPI and SQLModel. ✅
0 commit comments