diff --git a/docs/tutorial/where.md b/docs/tutorial/where.md index 5bc3139b57..5bc4057ef0 100644 --- a/docs/tutorial/where.md +++ b/docs/tutorial/where.md @@ -31,25 +31,7 @@ We'll continue with the same examples we have been using in the previous chapter And now we will update `select_heroes()` to filter the data. -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python hl_lines="36-41" -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="36-41" -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py hl[36:41] *} If you already executed the previous examples and have a database with data, **remove the database file** before running each example, that way you won't have duplicate data and you will be able to get the same results. @@ -201,97 +183,13 @@ Let's review some of the code we used to read data with **SQLModel**. We care specially about the **select** statement: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/select/tutorial001.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/select/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/select/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/select/tutorial001_py310.py ln[34:39] hl[5] *} ## Filter Rows Using `WHERE` with **SQLModel** Now, the same way that we add `WHERE` to a SQL statement to filter rows, we can add a `.where()` to a **SQLModel** `select()` statement to filter rows, which will filter the objects returned: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial001_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial001.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial001_py310.py ln[34:39] hl[5] *} It's a very small change, but it's packed of details. Let's explore them. @@ -537,49 +435,7 @@ Now that we know how `.where()` works, let's finish the code. It's actually the same as in previous chapters for selecting data: -//// tab | Python 3.10+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial001_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="6-8" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial001.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial001_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial001.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial001_py310.py ln[34:39] hl[6:8] *} We take that statement, that now includes a `WHERE`, and we `exec()` it to get the results. @@ -628,49 +484,7 @@ But we can use other standard Python comparisons. ✨ We could get the rows where a column is **not** equal to a value using `!=`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial002_py310.py[ln:34-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial002.py[ln:36-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial002_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial002.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial002_py310.py ln[34:39] hl[5] *} That would output: @@ -683,49 +497,7 @@ secret_name='Tommy Sharp' age=48 id=3 name='Rusty-Man' Let's update the function `create_heroes()` and add some more rows to make the next comparison examples clearer: -//// tab | Python 3.10+ - -```Python hl_lines="4-10 13-19" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003_py310.py[ln:21-39]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="4-10 13-19" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003.py[ln:23-41]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial003_py310.py ln[21:39] hl[4:10,13:19] *} Now that we have several heroes with different ages, it's gonna be more obvious what the next comparisons do. @@ -733,49 +505,7 @@ Now that we have several heroes with different ages, it's gonna be more obvious Now let's use `>` to get the rows where a column is **more than** a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial003.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial003_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial003.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial003_py310.py ln[42:47] hl[5] *} That would output: @@ -795,49 +525,7 @@ Notice that it didn't select `Black Lion`, because the age is not *strictly* gre Let's do that again, but with `>=` to get the rows where a column is **more than or equal** to a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial004_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial004.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial004_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial004.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial004_py310.py ln[42:47] hl[5] *} Because we are using `>=`, the age `35` will be included in the output: @@ -858,49 +546,7 @@ This time we got `Black Lion` too because although the age is not *strictly* gre Similarly, we can use `<` to get the rows where a column is **less than** a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial005_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial005.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial005_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial005.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial005_py310.py ln[42:47] hl[5] *} And we get the younger one with an age in the database: @@ -918,49 +564,7 @@ We could imagine that **Spider-Boy** is even **younger**. But because we don't k Finally, we can use `<=` to get the rows where a column is **less than or equal** to a value: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial006_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial006.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial006_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial006.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial006_py310.py ln[42:47] hl[5] *} And we get the younger ones, `35` and below: @@ -985,49 +589,7 @@ We can use the same standard Python comparison operators like `<`, `<=`, `>`, `> Because `.where()` returns the same special select object back, we can add more `.where()` calls to it: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial007_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial007.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial007_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial007.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial007_py310.py ln[42:47] hl[5] *} This will select the rows `WHERE` the `age` is **greater than or equal** to `35`, `AND` also the `age` is **less than** `40`. @@ -1068,49 +630,7 @@ age=36 id=6 name='Dr. Weird' secret_name='Steve Weird' As an alternative to using multiple `.where()` we can also pass several expressions to a single `.where()`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial008_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial008.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial008_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial008.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial008_py310.py ln[42:47] hl[5] *} This is the same as the above, and will result in the same output with the two heroes: @@ -1127,93 +647,13 @@ But we can also combine expressions using `OR`. Which means that **any** (but no To do it, you can import `or_`: -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/where/tutorial009_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/where/tutorial009.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial009_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial009.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial009_py310.py ln[1] hl[1] *} And then pass both expressions to `or_()` and put it inside `.where()`. For example, here we select the heroes that are the youngest OR the oldest: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial009_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial009.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial009_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial009.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial009_py310.py ln[42:47] hl[5] *} When we run it, this generates the output: @@ -1264,91 +704,11 @@ We can tell the editor that this class attribute is actually a special **SQLMode To do that, we can import `col()` (as short for "column"): -//// tab | Python 3.10+ - -```Python hl_lines="1" -{!./docs_src/tutorial/where/tutorial011_py310.py[ln:1]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="3" -{!./docs_src/tutorial/where/tutorial011.py[ln:1-3]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial011_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial011.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial011_py310.py ln[1] hl[1] *} And then put the **class attribute** inside `col()` when using it in a `.where()`: -//// tab | Python 3.10+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial011_py310.py[ln:42-47]!} - -# Code below omitted 👇 -``` - -//// - -//// tab | Python 3.7+ - -```Python hl_lines="5" -# Code above omitted 👆 - -{!./docs_src/tutorial/where/tutorial011.py[ln:44-49]!} - -# Code below omitted 👇 -``` - -//// - -/// details | 👀 Full file preview - -//// tab | Python 3.10+ - -```Python -{!./docs_src/tutorial/where/tutorial011_py310.py!} -``` - -//// - -//// tab | Python 3.7+ - -```Python -{!./docs_src/tutorial/where/tutorial011.py!} -``` - -//// - -/// +{* ./docs_src/tutorial/where/tutorial011_py310.py ln[42:47] hl[5] *} So, now the comparison is not: