From 6480f9f65650cbef868f14bcf66a4a237f01f61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antony=20Ar=C3=A9valo?= Date: Sat, 26 Oct 2024 19:04:08 -0500 Subject: [PATCH 1/2] Update includes for docs/advanced/decimal.md --- docs/advanced/decimal.md | 127 +-------------------------------------- 1 file changed, 3 insertions(+), 124 deletions(-) diff --git a/docs/advanced/decimal.md b/docs/advanced/decimal.md index 26994eccf8..2f73c7a8f0 100644 --- a/docs/advanced/decimal.md +++ b/docs/advanced/decimal.md @@ -33,45 +33,7 @@ For the database, **SQLModel** will use Date: Sat, 26 Oct 2024 22:09:38 -0500 Subject: [PATCH 2/2] Update automatic-id-none-refresh.md --- docs/tutorial/automatic-id-none-refresh.md | 369 +-------------------- 1 file changed, 9 insertions(+), 360 deletions(-) diff --git a/docs/tutorial/automatic-id-none-refresh.md b/docs/tutorial/automatic-id-none-refresh.md index 1963492359..f9ac023ea5 100644 --- a/docs/tutorial/automatic-id-none-refresh.md +++ b/docs/tutorial/automatic-id-none-refresh.md @@ -6,49 +6,7 @@ Now let's talk a bit about why the `id` field **can't be `NULL`** on the databas But the same `id` field actually **can be `None`** in the Python code, so we declare the type with `int | None (or Optional[int])`, and set the default value to `Field(default=None)`: -//// tab | Python 3.10+ - -```Python hl_lines="4" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:4-8]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="4" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:6-10]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[4:8] hl[4]*} Next, I'll show you a bit more about the synchronization of data between the database and the Python code. @@ -58,49 +16,7 @@ When do we get an actual `int` from the database in that `id` field? Let's see a When we create a new `Hero` instance, we don't set the `id`: -//// tab | Python 3.10+ - -```Python hl_lines="3-6" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-24]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3-6" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:23-26]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:24] hl[3:6]*} ### How `Optional` Helps @@ -128,49 +44,7 @@ But by declaring it with `Optional[int]`, the editor will help us to avoid writi We can confirm that by printing our heroes before adding them to the database: -//// tab | Python 3.10+ - -```Python hl_lines="9-11" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-29]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="9-11" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:23-31]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:29] hl[9:11]*} That will output: @@ -201,49 +75,7 @@ After we add the `Hero` instance objects to the **session**, the IDs are *still* We can verify by creating a session using a `with` block and adding the objects. And then printing them again: -//// tab | Python 3.10+ - -```Python hl_lines="19-21" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:21-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="19-21" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:23-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[21:39] hl[19:21]*} This will, again, output the `id`s of the objects as `None`: @@ -273,44 +105,7 @@ Then we can `commit` the changes in the session, and print again: ```Python hl_lines="13 16-18" # Code above omitted 👆 -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-46]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="13 16-18" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-48]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:46] hl[13,16:18]*} And now, something unexpected happens, look at the output, it seems as if the `Hero` instance objects had no data at all: @@ -368,49 +163,7 @@ We didn't access the object's attributes, like `hero.name`. We only accessed the To confirm and understand how this **automatic expiration and refresh** of data when accessing attributes work, we can print some individual fields (instance attributes): -//// tab | Python 3.10+ - -```Python hl_lines="21-23 26-28" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-56]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="21-23 26-28" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-58]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:56] hl[21:23, 26:28]*} Now we are actually accessing the attributes, because instead of printing the whole object `hero_1`: @@ -491,49 +244,7 @@ But what if you want to **explicitly refresh** the data? You can do that too with `session.refresh(object)`: -//// tab | Python 3.10+ - -```Python hl_lines="30-32 35-37" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-65]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="30-32 35-37" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-67]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:65] hl[30:32, 35:37]*} When Python executes this code: @@ -591,49 +302,7 @@ Now, as a final experiment, we can also print data after the **session** is clos There are no surprises here, it still works: -//// tab | Python 3.10+ - -```Python hl_lines="40-42" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py[ln:31-70]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="40-42" -# Code above omitted 👆 - -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-72]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py!} -``` - -//// - -/// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial001_py310.py ln[31:70] hl[40:42]*} And the output shows again the same data: @@ -666,27 +335,7 @@ Each one of the numbered bubbles shows what each line will print in the output. And as we created the **engine** with `echo=True`, we can see the SQL statements being executed at each step. -/// - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py!} -``` - -{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!} - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/automatic_id_none_refresh/tutorial002.py!} -``` - -{!./docs_src/tutorial/automatic_id_none_refresh/annotations/en/tutorial002.md!} - -//// +{*./docs_src/tutorial/automatic_id_none_refresh/tutorial002_py310.py*} And here's all the output generated by running this program, all together: