📝 Update markdown includes format (#1254)

This commit is contained in:
Sebastián Ramírez
2024-12-22 14:30:05 +00:00
committed by GitHub
parent 0c65fed61b
commit 5100200bea
39 changed files with 213 additions and 10659 deletions

View File

@@ -45,25 +45,7 @@ We will later update **Spider-Boy** to add him to the **Preventers** team too, b
We will continue with the code in the previous example and we will add more things to it.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[0] *}
Make sure you remove the `database.db` file before running the examples to get the same results.
@@ -75,49 +57,7 @@ And now we will also create the teams there. 🎉
Let's start by creating two teams:
//// tab | Python 3.10+
```Python hl_lines="3-9"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-35]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3-9"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:31-37]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:35] hl[29:35] *}
This would hopefully look already familiar.
@@ -133,49 +73,7 @@ And finally we **commit** the session to save the changes to the database.
Let's not forget to add this function `create_heroes()` to the `main()` function so that we run it when calling the program from the command line:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:61-63]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:63-65]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[61:63] hl[63] *}
## Run it
@@ -208,49 +106,7 @@ Now let's create one hero object to start.
As the `Hero` class model now has a field (column, attribute) `team_id`, we can set it by using the ID field from the `Team` objects we just created before:
//// tab | Python 3.10+
```Python hl_lines="12"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="12"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:31-41]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:39] hl[38] *}
We haven't committed this hero to the database yet, but there are already a couple of things to pay **attention** to.
@@ -274,49 +130,7 @@ INFO Engine [generated in 0.00025s] (2,)
Let's now create two more heroes:
//// tab | Python 3.10+
```Python hl_lines="14-20"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-50]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="14-20"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:31-52]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:50] hl[40:46] *}
When creating `hero_rusty_man`, we are accessing `team_preventers.id`, so that will also trigger a refresh of its data, generating an output of:
@@ -347,49 +161,7 @@ INFO Engine COMMIT
Now let's refresh and print those new heroes to see their new ID pointing to their teams:
//// tab | Python 3.10+
```Python hl_lines="26-28 30-32"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-58]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="26-28 30-32"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:31-60]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[29:58] hl[52:54,56:58] *}
If we execute that in the command line, it will output:

View File

@@ -57,45 +57,7 @@ Let's start by creating the tables in code.
Import the things we need from `sqlmodel` and create a new `Team` model:
//// tab | Python 3.10+
```Python hl_lines="4-7"
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py[ln:1-7]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6-9"
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:1-9]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[1:7] hl[4:7] *}
This is very similar to what we have been doing with the `Hero` model.
@@ -114,45 +76,7 @@ Now let's create the `hero` table.
This is the same model we have been using up to now, we are just adding the new column `team_id`:
//// tab | Python 3.10+
```Python hl_lines="16"
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py[ln:1-16]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="18"
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:1-18]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[1:16] hl[16] *}
Most of that should look familiar:
@@ -186,87 +110,11 @@ You can learn about setting a custom table name for a model in the Advanced User
Now we can add the same code as before to create the engine and the function to create the tables:
//// tab | Python 3.10+
```Python hl_lines="3-4 6 9-10"
# Code above omitted 👆
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py[ln:19-26]!}
```
////
//// tab | Python 3.7+
```Python hl_lines="3-4 6 9-10"
# Code above omitted 👆
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:21-28]!}
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[19:26] hl[19:20,22,25:26] *}
And as before, we'll call this function from another function `main()`, and we'll add that function `main()` to the main block of the file:
//// tab | Python 3.10+
```Python hl_lines="3-4 7-8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py[ln:29-34]!}
```
////
//// tab | Python 3.7+
```Python hl_lines="3-4 7-8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:31-36]!}
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/create_tables/tutorial001_py310.py ln[29:34] hl[29:30,33:34] *}
## Run the Code

View File

@@ -35,25 +35,7 @@ And the `hero` table has this data:
We will continue with the code in the previous example and we will add more things to it.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[0] *}
## `SELECT` Connected Data with SQL
@@ -135,49 +117,7 @@ Remember SQLModel's `select()` function? It can take more than one argument.
So, we can pass the `Hero` and `Team` model classes. And we can also use both their columns in the `.where()` part:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:61-63]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:63-65]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:63] hl[63] *}
Notice that in the comparison with `==` we are using the class attributes for both `Hero.team_id` and `Team.id`.
@@ -187,49 +127,7 @@ Now we can execute it and get the `results` object.
And as we used `select` with two models, we will receive tuples of instances of those two models, so we can iterate over them naturally in a `for` loop:
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:61-66]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:63-68]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:66] hl[65] *}
For each iteration in the `for` loop we get a a tuple with an instance of the class `Hero` and an instance of the class `Team`.
@@ -247,50 +145,7 @@ And you should get autocompletion and inline errors in your editor for both `her
As always, we must remember to add this new `select_heroes()` function to the `main()` function to make sure it is executed when we call this program from the command line.
//// tab | Python 3.10+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:69-72]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:71-74]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[69:72] hl[72] *}
## Run the Program
@@ -396,49 +251,7 @@ The same way there's a `.where()` available when using `select()`, there's also
And in SQLModel (actually SQLAlchemy), when using the `.join()`, because we already declared what is the `foreign_key` when creating the models, we don't have to pass an `ON` part, it is inferred automatically:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial002_py310.py[ln:61-66]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial002.py[ln:63-68]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial002_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial002_py310.py ln[61:66] hl[63] *}
Also notice that we are still including `Team` in the `select(Hero, Team)`, because we still want to access that data.
@@ -565,49 +378,7 @@ Now let's replicate the same query in **SQLModel**.
`.join()` has a parameter we can use `isouter=True` to make the `JOIN` be a `LEFT OUTER JOIN`:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial003_py310.py[ln:61-66]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial003.py[ln:63-68]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial003_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial003.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial003_py310.py ln[61:66] hl[63] *}
And if we run it, it will output:
@@ -654,49 +425,7 @@ But we would still be able to **filter** the rows with it. 🤓
We could even add some additional `.where()` after `.join()` to filter the data more, for example to return only the heroes from one team:
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial004_py310.py[ln:61-66]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial004.py[ln:63-68]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial004_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial004.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial004_py310.py ln[61:66] hl[63] *}
Here we are **filtering** with `.where()` to get only the heroes that belong to the **Preventers** team.
@@ -727,49 +456,7 @@ Preventer Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48
By putting the `Team` in `select()` we tell **SQLModel** and the database that we want the team data too.
//// tab | Python 3.10+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial005_py310.py[ln:61-66]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5"
# Code above omitted 👆
{!./docs_src/tutorial/connect/select/tutorial005.py[ln:63-68]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/select/tutorial005_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/select/tutorial005.py!}
```
////
///
{* ./docs_src/tutorial/connect/select/tutorial005_py310.py ln[61:66] hl[63] *}
And if we run that, it will output:

View File

@@ -35,25 +35,7 @@ Let's see how to **remove** connections between rows in tables.
We will continue with the code from the previous chapter.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/update/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/update/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/update/tutorial001_py310.py ln[0] *}
## Break a Connection
@@ -63,57 +45,7 @@ Let's say **Spider-Boy** is tired of the lack of friendly neighbors and wants to
We can simply set the `team_id` to `None`, and now it doesn't have a connection with the team:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/delete/tutorial001_py310.py[ln:29-30]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/connect/delete/tutorial001_py310.py[ln:66-70]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/delete/tutorial001.py[ln:31-32]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/connect/delete/tutorial001.py[ln:68-72]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/delete/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/delete/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/delete/tutorial001_py310.py ln[29:30,66:70] hl[66] *}
Again, we just **assign** a value to that field attribute `team_id`, now the value is `None`, which means `NULL` in the database. Then we `add()` the hero to the session, and then `commit()`.

View File

@@ -37,25 +37,7 @@ Now we'll see how to **update** those connections between rows tables.
We will continue with the code we used to create some heroes, and we'll update them.
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/insert/tutorial001_py310.py ln[0] *}
## Assign a Team to a Hero
@@ -63,57 +45,7 @@ Let's say that **Tommy Sharp** uses his "rich uncle" charms to recruit **Spider-
Doing it is just like updating any other field:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/update/tutorial001_py310.py[ln:29-30]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/connect/update/tutorial001_py310.py[ln:60-64]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/connect/update/tutorial001.py[ln:31-32]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/connect/update/tutorial001.py[ln:62-66]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/connect/update/tutorial001_py310.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/connect/update/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/connect/update/tutorial001_py310.py ln[29:30,60:64] hl[60] *}
We can simply **assign** a value to that field attribute `team_id`, then `add()` the hero to the session, and then `commit()`.