diff --git a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md b/docs/tutorial/relationship-attributes/cascade-delete-relationships.md index 1604b9780b..231a37cd08 100644 --- a/docs/tutorial/relationship-attributes/cascade-delete-relationships.md +++ b/docs/tutorial/relationship-attributes/cascade-delete-relationships.md @@ -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[7] *} ## 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[5,8,10,13] *} ## 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[7] *} 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[6] *} /// 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[6] *} ### Use `ondelete="RESTRICT"` @@ -1063,65 +528,7 @@ And in the `Team` model table, we will use `passive_deletes="all"` in the `Relat 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 @@ -1182,71 +589,7 @@ By calling the method `.clear()` from a list, we remove all its items. So, by ca Calling `team.heroes.clear()` is very similar to what SQLModel (actually SQLAlchemy) would have done if we didn't have `passive_deletes="all"` configured. -/// - -//// 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[7] *} ### Run the Program Deleting Heroes First