diff --git a/docs/tutorial/delete.md b/docs/tutorial/delete.md index 9cb6748b7c..c4b9b55f00 100644 --- a/docs/tutorial/delete.md +++ b/docs/tutorial/delete.md @@ -6,25 +6,7 @@ Now let's delete some data using **SQLModel**. As before, we'll continue from where we left off with the previous code. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/update/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/update/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/update/tutorial003_py310.py *} Remember to remove the `database.db` file before running the examples to get the same results. @@ -74,91 +56,11 @@ To get the same results, delete the `database.db` file before running the exampl We'll start by selecting the hero `"Spider-Youngster"` that we updated in the previous chapter, this is the one we will delete: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-75]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-77]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:75] hl[5] *} As this is a new function `delete_heroes()`, we'll also add it to the `main()` function so that we call it when executing the program from the command line: -//// tab | Python 3.10+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:90-98]!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="7" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:92-100]!} -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[90:98] hl[7] *} That will print the same existing hero **Spider-Youngster**: @@ -186,49 +88,7 @@ Hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 Now, very similar to how we used `session.add()` to add or update new heroes, we can use `session.delete()` to delete the hero from the session: -//// tab | Python 3.10+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-77]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="10" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-79]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:77] hl[10] *} ## Commit the Session @@ -236,49 +96,7 @@ To save the current changes in the session, **commit** it. This will save all the changes stored in the **session**, like the deleted hero: -//// tab | Python 3.10+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-78]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="11" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-80]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:78] hl[11] *} The same as we have seen before, `.commit()` will also save anything else that was added to the session. Including updates, or created heroes. @@ -313,49 +131,7 @@ As the object is not connected to the session, it is not marked as "expired", th Because of that, the object still contains its attributes with the data in it, so we can print it: -//// tab | Python 3.10+ - -```Python hl_lines="13" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-80]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-82]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:80] hl[13] *} This will output: @@ -378,49 +154,7 @@ Deleted hero: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2 To confirm if it was deleted, now let's query the database again, with the same `"Spider-Youngster"` name: -//// tab | Python 3.10+ - -```Python hl_lines="15-17" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-84]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="15-17" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-86]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:84] hl[15:17] *} Here we are using `results.first()` to get the first object found (in case it found multiple) or `None`, if it didn't find anything. @@ -457,49 +191,7 @@ Now let's just confirm that, indeed, no hero was found in the database with that We'll do it by checking that the "first" item in the `results` is `None`: -//// tab | Python 3.10+ - -```Python hl_lines="19-20" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001_py310.py[ln:70-87]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="19-20" -# Code above omitted 👆 - -{!./docs_src/tutorial/delete/tutorial001.py[ln:72-89]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/delete/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/delete/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/delete/tutorial001_py310.py ln[70:87] hl[19:20] *} This will output: @@ -525,25 +217,7 @@ INFO Engine ROLLBACK Now let's review all that code: -//// tab | Python 3.10+ - -```{ .python .annotate hl_lines="70-88" } -{!./docs_src/tutorial/delete/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/delete/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.7+ - -```{ .python .annotate hl_lines="72-90" } -{!./docs_src/tutorial/delete/tutorial002.py!} -``` - -{!./docs_src/tutorial/delete/annotations/en/tutorial002.md!} - -//// +{* ./docs_src/tutorial/delete/tutorial002_py310.py hl[70:88] *} /// tip