mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-12-16 01:59:03 +08:00
📝 Update details syntax with new pymdown extensions format (#713)
This commit is contained in:
committed by
GitHub
parent
be464fba69
commit
799d0aa7a6
@@ -45,14 +45,13 @@ We will later update **Spider-Boy** to add him to the **Preventers** team too, b
|
||||
|
||||
We will continue with the code in the previous example and we will add more things to it.
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Make sure you remove the `database.db` file before running the examples to get the same results.
|
||||
|
||||
@@ -72,14 +71,13 @@ Let's start by creating two teams:
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
This would hopefully look already familiar.
|
||||
|
||||
@@ -103,14 +101,13 @@ Let's not forget to add this function `create_heroes()` to the `main()` function
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
## Run it
|
||||
|
||||
@@ -151,14 +148,13 @@ As the `Hero` class model now has a field (column, attribute) `team_id`, we can
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
We haven't committed this hero to the database yet, but there are already a couple of things to pay **attention** to.
|
||||
|
||||
@@ -190,14 +186,13 @@ Let's now create two more heroes:
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
When creating `hero_rusty_man`, we are accessing `team_preventers.id`, so that will also trigger a refresh of its data, generating an output of:
|
||||
|
||||
@@ -236,14 +231,13 @@ Now let's refresh and print those new heroes to see their new ID pointing to the
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
|
||||
If we execute that in the command line, it will output:
|
||||
|
||||
@@ -63,14 +63,13 @@ Import the things we need from `sqlmodel` and create a new `Team` model:
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
This is very similar to what we have been doing with the `Hero` model.
|
||||
|
||||
@@ -95,14 +94,13 @@ This is the same model we have been using up to now, we are just adding the new
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Most of that should look familiar:
|
||||
|
||||
@@ -142,14 +140,13 @@ Now we can add the same code as before to create the engine and the function to
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:21-28]!}
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
And as before, we'll call this function from another function `main()`, and we'll add that function `main()` to the main block of the file:
|
||||
|
||||
@@ -159,14 +156,13 @@ And as before, we'll call this function from another function `main()`, and we'l
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py[ln:31-36]!}
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/create_tables/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
## Run the Code
|
||||
|
||||
|
||||
@@ -35,14 +35,13 @@ And the `hero` table has this data:
|
||||
|
||||
We will continue with the code in the previous example and we will add more things to it.
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
## `SELECT` Connected Data with SQL
|
||||
|
||||
@@ -132,14 +131,13 @@ So, we can pass the `Hero` and `Team` model classes. And we can also use both th
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Notice that in the comparison with `==` we are using the class attributes for both `Hero.team_id` and `Team.id`.
|
||||
|
||||
@@ -157,14 +155,13 @@ And as we used `select` with two models, we will receive tuples of instances of
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
For each iteration in the `for` loop we get a a tuple with an instance of the class `Hero` and an instance of the class `Team`.
|
||||
|
||||
@@ -190,14 +187,13 @@ As always, we must remember to add this new `select_heroes()` function to the `m
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
|
||||
## Run the Program
|
||||
@@ -312,14 +308,13 @@ And in SQLModel (actually SQLAlchemy), when using the `.join()`, because we alre
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial002.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Also notice that we are still including `Team` in the `select(Hero, Team)`, because we still want to access that data.
|
||||
|
||||
@@ -454,14 +449,13 @@ Now let's replicate the same query in **SQLModel**.
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial003.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
And if we run it, it will output:
|
||||
|
||||
@@ -516,14 +510,13 @@ We could even add some additional `.where()` after `.join()` to filter the data
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial004.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Here we are **filtering** with `.where()` to get only the heroes that belong to the **Preventers** team.
|
||||
|
||||
@@ -562,14 +555,13 @@ By putting the `Team` in `select()` we tell **SQLModel** and the database that w
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/select/tutorial005.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
And if we run that, it will output:
|
||||
|
||||
|
||||
@@ -35,14 +35,13 @@ Let's see how to **remove** connections between rows in tables.
|
||||
|
||||
We will continue with the code from the previous chapter.
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/update/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
## Break a Connection
|
||||
|
||||
@@ -64,14 +63,13 @@ We can simply set the `team_id` to `None`, and now it doesn't have a connection
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/delete/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
Again, we just **assign** a value to that field attribute `team_id`, now the value is `None`, which means `NULL` in the database. Then we `add()` the hero to the session, and then `commit()`.
|
||||
|
||||
|
||||
@@ -37,14 +37,13 @@ Now we'll see how to **update** those connections between rows tables.
|
||||
|
||||
We will continue with the code we used to create some heroes, and we'll update them.
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
## Assign a Team to a Hero
|
||||
|
||||
@@ -64,14 +63,13 @@ Doing it is just like updating any other field:
|
||||
# Code below omitted 👇
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>👀 Full file preview</summary>
|
||||
/// details | 👀 Full file preview
|
||||
|
||||
```Python
|
||||
{!./docs_src/tutorial/connect/update/tutorial001.py!}
|
||||
```
|
||||
|
||||
</details>
|
||||
///
|
||||
|
||||
We can simply **assign** a value to that field attribute `team_id`, then `add()` the hero to the session, and then `commit()`.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user