📝 Add docs annotations for source examples

This commit is contained in:
Sebastián Ramírez
2021-08-24 14:51:14 +02:00
parent e6308146f7
commit dc331f649c
13 changed files with 1074 additions and 0 deletions

View File

@ -0,0 +1,68 @@
1. Select the hero we will work with.
2. Execute the query with the select statement object.
This generates the output:
```
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.name = ?
INFO Engine [no key 0.00017s] ('Spider-Boy',)
```
3. Get one hero object, expecting exactly one.
!!! tip
This ensures there's no more than one, and that there's exactly one, not `None`.
This would never return `None`, instead it would raise an exception.
4. Print the hero object.
This generates the output:
```
Hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2
```
5. Set the hero's age field to the new value `16`.
Now the `hero` object in memory has a different value for the age, but it is still not saved to the database.
6. Add the hero to the session.
This puts it in that temporary place in the session before committing.
But it's still not saved in the database yet.
7. Commit the session.
This saves the updated hero to the database.
And this generates the output:
```
INFO Engine UPDATE hero SET age=? WHERE hero.id = ?
INFO Engine [generated in 0.00017s] (16, 2)
INFO Engine COMMIT
```
8. Refresh the hero object to have the recent data, including the age we just committed.
This generates the output:
```
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.id = ?
INFO Engine [generated in 0.00018s] (2,)
```
9. Print the updated hero object.
This generates the output:
```
Updated hero: name='Spider-Boy' secret_name='Pedro Parqueador' age=16 id=2
```

View File

@ -0,0 +1,159 @@
1. Select the hero `Spider-Boy`.
2. Execute the select statement.
This generates the output:
```
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.name = ?
INFO Engine [no key 0.00018s] ('Spider-Boy',)
```
3. Get one hero object, the only one that should be there for **Spider-Boy**.
4. Print this hero.
This generates the output:
```
Hero 1: name='Spider-Boy' secret_name='Pedro Parqueador' age=None id=2
```
5. Select another hero.
6. Execute the select statement.
This generates the output:
```
INFO Engine BEGIN (implicit)
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.name = ?
INFO Engine [no key 0.00020s] ('Captain North America',)
```
!!! tip
See the `BEGIN` at the top?
This is SQLAlchemy automatically starting a transaction for us.
This way, we could revert the last changes (if there were some) if we wanted to, even if the SQL to create them was already sent to the database.
7. Get one hero object for this new query.
The only one that should be there for **Captain North America**.
8. Print this second hero.
This generates the output:
```
Hero 2: name='Captain North America' secret_name='Esteban Rogelios' age=93 id=7
```
9. Update the age for the first hero.
Set the value of the attribute `age` to `16`.
This updates the hero object in memory, but not yet in the database.
10. Update the name of the first hero.
Now the name of the hero will not be `"Spider-Boy"` but `"Spider-Youngster"`.
Also, this updates the object in memory, but not yet in the database.
11. Add this first hero to the session.
This puts it in the temporary space in the **session** before committing it to the database.
It is not saved yet.
12. Update the name of the second hero.
Now the hero has a bit more precision in the name. 😜
This updates the object in memory, but not yet in the database.
13. Update the age of the second hero.
This updates the object in memory, but not yet in the database.
14. Add the second hero to the session.
This puts it in the temporary space in the **session** before committing it to the database.
15. Commit all the changes tracked in the session.
This commits everything in one single batch.
This generates the output:
```
INFO Engine UPDATE hero SET name=?, age=? WHERE hero.id = ?
INFO Engine [generated in 0.00028s] (('Spider-Youngster', 16, 2), ('Captain North America Except Canada', 110, 7))
INFO Engine COMMIT
```
!!! tip
See how SQLAlchemy (that powers SQLModel) optimizes the SQL to do as much work as possible in a single batch.
Here it updates both heroes in a single SQL query.
16. Refresh the first hero.
This generates the output:
```
INFO Engine BEGIN (implicit)
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.id = ?
INFO Engine [generated in 0.00023s] (2,)
```
!!! tip
Because we just committed a SQL transaction with `COMMIT`, SQLAlchemy will automatically start a new transaction with `BEGIN`.
17. Refresh the second hero.
This generates the output:
```
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
FROM hero
WHERE hero.id = ?
INFO Engine [cached since 0.001709s ago] (7,)
```
!!! tip
SQLAlchemy is still using the previous transaction, so it doesn't have to create a new one.
18. Print the first hero, now udpated.
This generates the output:
```
Updated hero 1: name='Spider-Youngster' secret_name='Pedro Parqueador' age=16 id=2
```
19. Print the second hero, now updated.
This generates the output:
```
Updated hero 2: name='Captain North America Except Canada' secret_name='Esteban Rogelios' age=110 id=7
```
20. Here is the end of the `with` block statement, so the session can execute its terminating code.
The session will `ROLLBACK` (undo) any possible changes in the last transaction that were not committed.
This generates the output:
```
INFO Engine ROLLBACK
```