Add source examples for Python 3.10 and 3.9 with updated syntax (#842)

Co-authored-by: Esteban Maya Cadavid <emayacadavid9@gmail.com>
This commit is contained in:
Sebastián Ramírez
2024-03-21 17:49:38 -05:00
committed by GitHub
parent 4c3f242ae2
commit 9141c8a920
39 changed files with 7456 additions and 25 deletions

View File

@@ -47,10 +47,22 @@ We will continue with the code in the previous example and we will add more thin
/// 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!}
```
////
///
Make sure you remove the `database.db` file before running the examples to get the same results.
@@ -63,6 +75,20 @@ 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 👆
@@ -71,12 +97,26 @@ Let's start by creating two teams:
# 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!}
```
////
///
This would hopefully look already familiar.
@@ -93,6 +133,20 @@ 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 👆
@@ -101,12 +155,26 @@ Let's not forget to add this function `create_heroes()` to the `main()` function
# 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!}
```
////
///
## Run it
@@ -140,6 +208,20 @@ 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 👆
@@ -148,12 +230,26 @@ As the `Hero` class model now has a field (column, attribute) `team_id`, we can
# 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!}
```
////
///
We haven't committed this hero to the database yet, but there are already a couple of things to pay **attention** to.
@@ -178,6 +274,20 @@ 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 👆
@@ -186,12 +296,26 @@ Let's now create two more heroes:
# 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!}
```
////
///
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:
@@ -223,7 +347,21 @@ INFO Engine COMMIT
Now let's refresh and print those new heroes to see their new ID pointing to their teams:
```Python hl_lines="26-28 30-32"
//// 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]!}
@@ -231,14 +369,27 @@ Now let's refresh and print those new heroes to see their new ID pointing to the
# 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!}
```
///
////
///
If we execute that in the command line, it will output:

View File

@@ -57,18 +57,44 @@ 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!}
```
////
///
This is very similar to what we have been doing with the `Hero` model.
@@ -88,18 +114,44 @@ 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!}
```
////
///
Most of that should look familiar:
@@ -134,34 +186,86 @@ 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!}
```
////
///
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!}
```
////
///
## Run the Code

View File

@@ -37,10 +37,22 @@ We will continue with the code in the previous example and we will add more thin
/// 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!}
```
////
///
## `SELECT` Connected Data with SQL
@@ -123,6 +135,20 @@ 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 👆
@@ -131,12 +157,26 @@ So, we can pass the `Hero` and `Team` model classes. And we can also use both th
# 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!}
```
////
///
Notice that in the comparison with `==` we are using the class attributes for both `Hero.team_id` and `Team.id`.
@@ -147,6 +187,20 @@ 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 👆
@@ -155,12 +209,26 @@ And as we used `select` with two models, we will receive tuples of instances of
# 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!}
```
////
///
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`.
@@ -179,6 +247,20 @@ 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 👆
@@ -187,12 +269,26 @@ As always, we must remember to add this new `select_heroes()` function to the `m
# 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!}
```
////
///
@@ -300,6 +396,20 @@ 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 👆
@@ -308,12 +418,26 @@ And in SQLModel (actually SQLAlchemy), when using the `.join()`, because we alre
# 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!}
```
////
///
Also notice that we are still including `Team` in the `select(Hero, Team)`, because we still want to access that data.
@@ -441,6 +565,20 @@ 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 👆
@@ -449,12 +587,26 @@ Now let's replicate the same query in **SQLModel**.
# 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!}
```
////
///
And if we run it, it will output:
@@ -502,6 +654,20 @@ 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 👆
@@ -510,12 +676,26 @@ We could even add some additional `.where()` after `.join()` to filter the data
# 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!}
```
////
///
Here we are **filtering** with `.where()` to get only the heroes that belong to the **Preventers** team.
@@ -547,6 +727,20 @@ 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 👆
@@ -555,12 +749,26 @@ By putting the `Team` in `select()` we tell **SQLModel** and the database that w
# 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!}
```
////
///
And if we run that, it will output:

View File

@@ -37,10 +37,22 @@ 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!}
```
////
///
## Break a Connection
@@ -51,6 +63,24 @@ 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 👆
@@ -63,12 +93,26 @@ We can simply set the `team_id` to `None`, and now it doesn't have a connection
# 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!}
```
////
///
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

@@ -39,10 +39,22 @@ We will continue with the code we used to create some heroes, and we'll update t
/// 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!}
```
////
///
## Assign a Team to a Hero
@@ -51,6 +63,24 @@ 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 👆
@@ -63,12 +93,26 @@ Doing it is just like updating any other field:
# 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!}
```
////
///
We can simply **assign** a value to that field attribute `team_id`, then `add()` the hero to the session, and then `commit()`.