📝 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

@@ -20,63 +20,7 @@ Let's understand that better with an example.
Let's see how that works by writing an **incomplete** version first, without `back_populates`:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[1:19] hl[9,19] *}
## Read Data Objects
@@ -84,69 +28,7 @@ Now, we will get the **Spider-Boy** hero and, *independently*, the **Preventers*
As you already know how this works, I won't separate that in a select `statement`, `results`, etc. Let's use the shorter form in a single call:
//// tab | Python 3.10+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-111]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-113]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5-7 9-11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-113]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:111] hl[105:107,109:111] *}
/// tip
@@ -158,69 +40,7 @@ When writing your own code, this is probably the style you will use most often,
Now, let's print the current **Spider-Boy**, the current **Preventers** team, and particularly, the current **Preventers** list of heroes:
//// tab | Python 3.10+
```Python hl_lines="13-15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-115]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="13-15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-117]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="13-15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-117]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:115] hl[113:115] *}
Up to this point, it's all good. 😊
@@ -242,81 +62,7 @@ Notice that we have **Spider-Boy** there.
Now let's update **Spider-Boy**, removing him from the team by setting `hero_spider_boy.team = None` and then let's print this object again:
//// tab | Python 3.10+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:119-123]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,117:121] hl[117,121] *}
The first important thing is, we *haven't committed* the hero yet, so accessing the list of heroes would not trigger an automatic refresh.
@@ -356,81 +102,7 @@ Oh, no! 😱 **Spider-Boy** is still listed there!
Now, if we commit it and print again:
//// tab | Python 3.10+
```Python hl_lines="8-9 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py[ln:123-130]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8-9 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py[ln:125-132]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8-9 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py[ln:125-132]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial001_py310.py ln[103:104,123:130] hl[123:124,130] *}
When we access `preventers_team.heroes` after the `commit`, that triggers a refresh, so we get the latest list, without **Spider-Boy**, so that's fine again:
@@ -462,141 +134,11 @@ That's what `back_populates` is for. ✨
Let's add it back:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *}
And we can keep the rest of the code the same:
//// tab | Python 3.10+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:103-104]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8 12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:105-106]!}
# Code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:119-123]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[103:104,117:121] hl[117,121] *}
/// tip
@@ -629,63 +171,7 @@ Now that you know why `back_populates` is there, let's review the exact value ag
It's quite simple code, it's just a string, but it might be confusing to think exactly *what* string should go there:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[1:19] hl[9,19] *}
The string in `back_populates` is the name of the attribute *in the other* model, that will reference *the current* model.
@@ -693,69 +179,7 @@ The string in `back_populates` is the name of the attribute *in the other* model
So, in the class `Team`, we have an attribute `heroes` and we declare it with `Relationship(back_populates="team")`.
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:4-9]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:6-11]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:6-11]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[4:9] hl[9] *}
The string in `back_populates="team"` refers to the attribute `team` in the class `Hero` (the other class).
@@ -763,69 +187,7 @@ And, in the class `Hero`, we declare an attribute `team`, and we declare it with
So, the string `"heroes"` refers to the attribute `heroes` in the class `Team`.
//// tab | Python 3.10+
```Python hl_lines="10"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py[ln:12-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="10"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py[ln:14-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="10"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py[ln:14-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial002_py310.py ln[12:19] hl[19] *}
/// tip
@@ -850,66 +212,4 @@ So, `back_populates` would most probably be something like `"hero"` or `"heroes"
<img src="/img/tutorial/relationships/attributes/back-populates2.svg">
//// tab | Python 3.10+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py[ln:27-39]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py[ln:29-41]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3 10 13 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py[ln:29-41]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/back_populates/tutorial003.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/back_populates/tutorial003_py310.py ln[27:39] hl[27,34,37,39] *}

View File

@@ -109,63 +109,7 @@ When creating a `Relationship()`, we can set `cascade_delete=True`.
This configures SQLModel to **automatically delete** the related records (heroes) **when the initial one is deleted** (a team).
//// tab | Python 3.10+
```Python hl_lines="9"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:1-9]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:1-11]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:1-11]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:9] hl[9] *}
With this configuration, when we delete a team, SQLModel (actually SQLAlchemy) will:
@@ -211,63 +155,7 @@ The `ondelete` parameter will set a SQL `ON DELETE` in the **foreign key column*
If we want to configure the database to **automatically delete** the related records when the parent is deleted, we can set `ondelete="CASCADE"`.
//// tab | Python 3.10+
```Python hl_lines="18"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:1-23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:1-23]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[1:19] hl[18] *}
Now, when we **create the tables** in the database, the `team_id` column in the `Hero` table will have an `ON DELETE CASCADE` in its definition at the database level.
@@ -331,69 +219,7 @@ class Team(SQLModel, table=True):
Now, when we **delete a team**, we don't need to do anything else, it's **automatically** going to **delete its heroes**.
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:76-82]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:80-86]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:80-86]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[76:82] hl[80] *}
## Confirm Heroes are Deleted
@@ -401,69 +227,7 @@ We can confirm that **after deleting the team** `Wakaland`, the heroes `Black Li
If we try to select them from the database, we will **no longer find them**.
//// tab | Python 3.10+
```Python hl_lines="5 8 10 13"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py[ln:85-95]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="5 8 10 13"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py[ln:89-99]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="5 8 10 13"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py[ln:89-99]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial001_py310.py ln[85:95] hl[87,90,92,95] *}
## Run the Program with `cascade_delete=True` and `ondelete="CASCADE"`
@@ -533,64 +297,7 @@ We can configure the database to **set the foreign key** (the `team_id` in the `
In this case, the side with `Relationship()` won't have `cascade_delete`, but the side with `Field()` and a `foreign_key` will have `ondelete="SET NULL"`.
//// tab | Python 3.10+
```Python hl_lines="19"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py[ln:1-23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py[ln:1-23]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[1:21] hl[19] *}
The configuration above is setting the `team_id` column from the `Hero` table to have an `ON DELETE SET NULL`.
@@ -626,69 +333,7 @@ But if you delete a team from code, by default, SQLModel (actually SQLAlchemy) w
Removing a team has the **same code** as before, the only thing that changes is the configuration underneath in the database.
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py[ln:78-84]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py[ln:80-86]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py[ln:80-86]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial002_py310.py ln[78:84] hl[82] *}
The result would be these tables.
@@ -793,69 +438,7 @@ If you know your database would be able to correctly handle the deletes or updat
To be able to test this out with SQLite, we first need to enable foreign key support.
//// tab | Python 3.10+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py[ln:30-33]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py[ln:32-35]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py[ln:32-35]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[30:33] hl[33] *}
/// info
@@ -869,63 +452,7 @@ Now let's update the table model for `Team` to use `passive_deletes="all"` in th
We will also use `ondelete="SET NULL"` in the `Hero` model table, in the foreign key `Field()` for the `team_id` to make the database set those fields to `NULL` automatically.
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py[ln:1-23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py[ln:1-23]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial003_py310.py ln[1:21] hl[9,19] *}
### Run the Program with `passive_deletes`
@@ -989,69 +516,7 @@ For SQLite, this also needs enabling foreign key support.
As `ondelete="RESTRICT"` is mainly a database-level constraint, let's enable foreign key support in SQLite first to be able to test it.
//// tab | Python 3.10+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py[ln:30-33]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py[ln:32-35]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="6"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py[ln:32-35]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[30:33] hl[33] *}
### Use `ondelete="RESTRICT"`
@@ -1065,63 +530,7 @@ Notice that we don't set `cascade_delete` in the `Team` model table.
///
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py[ln:1-23]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py[ln:1-23]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial004_py310.py ln[1:21] hl[9,19] *}
### Run the Program with `RESTRICT`, See the Error
@@ -1184,69 +593,7 @@ Calling `team.heroes.clear()` is very similar to what SQLModel (actually SQLAlch
///
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py[ln:80-88]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py[ln:82-90]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py[ln:82-90]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/cascade_delete_relationships/tutorial005_py310.py ln[80:88] hl[84] *}
### Run the Program Deleting Heroes First

View File

@@ -6,49 +6,7 @@ Let's see now how to create data with relationships using these new **relationsh
Let's check the old code we used to create some heroes and teams:
//// tab | Python 3.10+
```Python hl_lines="9 12 18 24"
# Code above omitted 👆
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:29-58]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9 12 18 24"
# 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[35,38,44,50] *}
There are several things to **notice** here.
@@ -68,69 +26,7 @@ This is the first area where these **relationship attributes** can help. 🤓
Now let's do all that, but this time using the new, shiny `Relationship` attributes:
//// tab | Python 3.10+
```Python hl_lines="9 12 18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:32-55]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9 12 18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:34-57]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9 12 18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:34-57]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[32:55] hl[38,41,47] *}
Now we can create the `Team` instances and pass them directly to the new `team` argument when creating the `Hero` instances, as `team=team_preventers` instead of `team_id=team_preventers.id`.
@@ -146,81 +42,7 @@ And then, as you can see, we only have to do one `commit()`.
The same way we could assign an integer with a `team.id` to a `hero.team_id`, we can also assign the `Team` instance to the `hero.team`:
//// tab | Python 3.10+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:57-61]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:59-63]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="8"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:59-63]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,57:61] hl[57] *}
## Create a Team with Heroes
@@ -228,81 +50,7 @@ Before, we created some `Team` instances and passed them in the `team=` argument
We could also create the `Hero` instances first, and then pass them in the `heroes=` argument that takes a list, when creating a `Team` instance:
//// tab | Python 3.10+
```Python hl_lines="13 15-16"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:63-73]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="13 15-16"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:65-75]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="13 15-16"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:65-75]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,63:73] hl[68,70:71] *}
Here we create two heroes first, **Black Lion** and **Princess Sure-E**, and then we pass them in the `heroes` argument.
@@ -318,81 +66,7 @@ As the attribute `team.heroes` behaves like a list, we can simply append to it.
Let's create some more heroes and add them to the `team_preventers.heroes` list attribute:
//// tab | Python 3.10+
```Python hl_lines="14-18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:32-33]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py[ln:75-91]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="14-18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py[ln:77-93]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="14-18"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:34-35]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py[ln:77-93]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/create_and_update_relationships/tutorial001_py310.py ln[32:33,75:91] hl[81:85] *}
The attribute `team_preventers.heroes` behaves like a list. But it's a special type of list, because when we modify it adding heroes to it, **SQLModel** (actually SQLAlchemy) **keeps track of the necessary changes** to be done in the database.

View File

@@ -41,45 +41,7 @@ Now that you know how these tables work underneath and how the model classes rep
Up to now, we have only used the `team_id` column to connect the tables when querying with `select()`:
//// tab | Python 3.10+
```Python hl_lines="16"
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py[ln:1-16]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="18"
{!./docs_src/tutorial/connect/insert/tutorial001.py[ln:1-18]!}
# 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[1:16] hl[16] *}
This is a **plain field** like all the others, all representing a **column in the table**.
@@ -87,123 +49,11 @@ But now let's add a couple of new special attributes to these model classes, let
First, import `Relationship` from `sqlmodel`:
//// tab | Python 3.10+
```Python hl_lines="1"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-3]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-3]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1] hl[1] *}
Next, use that `Relationship` to declare a new attribute in the model classes:
//// tab | Python 3.10+
```Python hl_lines="9 19"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11 21"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9,19] *}
## What Are These Relationship Attributes

View File

@@ -6,81 +6,7 @@ Now that we know how to connect data using **relationship Attributes**, let's se
First, add a function `select_heroes()` where we get a hero to start working with, and add that function to the `main()` function:
//// tab | Python 3.10+
```Python hl_lines="3-7 14"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:108-111]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="3-7 14"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:110-113]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="3-7 14"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-100]!}
# Previous code here omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:110-113]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,108:111] hl[94:98,111] *}
## Select the Related Team - Old Way
@@ -88,69 +14,7 @@ Now that we have a hero, we can get the team this hero belongs to.
With what we have learned **up to now**, we could use a `select()` statement, then execute it with `session.exec()`, and then get the `.first()` result, for example:
//// tab | Python 3.10+
```Python hl_lines="9-12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-103]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9-12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-105]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9-12"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-105]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:103] hl[100:103] *}
## Get Relationship Team - New Way
@@ -158,81 +22,7 @@ But now that we have the **relationship attributes**, we can just access them, a
So, the highlighted block above, has the same results as the block below:
//// tab | Python 3.10+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:94-98]!}
# Code from the previous example omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py[ln:105]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:96-100]!}
# Code from the previous example omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py[ln:107]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:96-100]!}
# Code from the previous example omitted 👈
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py[ln:107]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial001_py310.py ln[94:98,105] hl[105] *}
/// tip
@@ -246,69 +36,7 @@ For example, here, **inside** a `with` block with a `Session` object.
And the same way, when we are working on the **many** side of the **one-to-many** relationship, we can get a list of of the related objects just by accessing the relationship attribute:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:94-100]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:96-102]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:96-102]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[94:100] hl[100] *}
That would print a list with all the heroes in the Preventers team:

View File

@@ -8,135 +8,11 @@ And then for some reason needs to leave the **Preventers** for some years. 😭
We can remove the relationship by setting it to `None`, the same as with the `team_id`, it also works with the new relationship attribute `.team`:
//// tab | Python 3.10+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:103-114]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:105-116]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="9"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:105-116]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[103:114] hl[109] *}
And of course, we should remember to add this `update_heroes()` function to `main()` so that it runs when we call this program from the command line:
//// tab | Python 3.10+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py[ln:117-121]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py[ln:119-123]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="7"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py[ln:119-123]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/read_relationships/tutorial002_py310.py ln[117:121] hl[121] *}
## Recap

View File

@@ -2,63 +2,7 @@
In the first Relationship attribute, we declare it with `List["Hero"]`, putting the `Hero` in quotes instead of just normally there:
//// tab | Python 3.10+
```Python hl_lines="9"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py[ln:1-19]!}
# Code below omitted 👇
```
////
//// tab | Python 3.9+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py[ln:1-21]!}
# Code below omitted 👇
```
////
//// tab | Python 3.7+
```Python hl_lines="11"
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py[ln:1-21]!}
# Code below omitted 👇
```
////
/// details | 👀 Full file preview
//// tab | Python 3.10+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py!}
```
////
//// tab | Python 3.9+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py39.py!}
```
////
//// tab | Python 3.7+
```Python
{!./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001.py!}
```
////
///
{* ./docs_src/tutorial/relationship_attributes/define_relationship_attributes/tutorial001_py310.py ln[1:19] hl[9] *}
What's that about? Can't we just write it normally as `List[Hero]`?