FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

📝 Add docs annotations for source examples · mastercoms/sqlmodel@dc331f6 · GitHub

Repository navigation

Commit dc331f6

Browse files
committed
📝 Add docs annotations for source examples
1 parent e630814 commit dc331f6

13 files changed

Lines changed: 1074 additions & 0 deletions

File tree

Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
1. Create the `hero_1`.
2+
3+
**Doesn't generate any output**.
4+
5+
2. Create the `hero_2`.
6+
7+
**Doesn't generate any output**.
8+
9+
3. Create the `hero_3`.
10+
11+
**Doesn't generate any output**.
12+
13+
4. Print the line `"Before interacting with the database"`.
14+
15+
Generates the output:
16+
17+
```
18+
Before interacting with the database
19+
```
20+
21+
5. Print the `hero_1` before interacting with the database.
22+
23+
Generates the output:
24+
25+
```
26+
Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None
27+
```
28+
29+
6. Print the `hero_2` before interacting with the database.
30+
31+
Generates the output:
32+
33+
```
34+
Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None
35+
```
36+
37+
7. Print the `hero_3` before interacting with the database.
38+
39+
Generates the output:
40+
41+
```
42+
Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48
43+
```
44+
45+
8. Create the `Session` in a `with` block.
46+
47+
**Doesn't generate any output**.
48+
49+
9. Add the `hero_1` to the session.
50+
51+
This still doesn't save it to the database.
52+
53+
**Doesn't generate any output**.
54+
55+
10. Add the `hero_2` to the session.
56+
57+
This still doesn't save it to the database.
58+
59+
**Doesn't generate any output**.
60+
61+
11. Add the `hero_3` to the session.
62+
63+
This still doesn't save it to the database.
64+
65+
**Doesn't generate any output**.
66+
67+
12. Print the line `"After adding to the session"`.
68+
69+
Generates the output:
70+
71+
```
72+
After adding to the session
73+
```
74+
75+
13. Print the `hero_1` after adding it to the session.
76+
77+
It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`.
78+
79+
Generates the output:
80+
81+
```
82+
Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None
83+
```
84+
85+
14. Print the `hero_2` after adding it to the session.
86+
87+
It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`.
88+
89+
Generates the output:
90+
91+
```
92+
Hero 2: id=None name='Spider-Boy' secret_name='Pedro Parqueador' age=None
93+
```
94+
95+
15. Print the `hero_3` after adding it to the session.
96+
97+
It still has the same data as there hasn't been any interaction with the database yet. Notice that the `id` is still `None`.
98+
99+
Generates the output:
100+
101+
```
102+
Hero 3: id=None name='Rusty-Man' secret_name='Tommy Sharp' age=48
103+
```
104+
105+
16. `commit` the **session**.
106+
107+
This will **save** all the data to the database. The **session** will use the **engine** to run a lot of SQL.
108+
109+
Generates the output:
110+
111+
```
112+
INFO Engine BEGIN (implicit)
113+
INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?)
114+
INFO Engine [generated in 0.00018s] ('Deadpond', 'Dive Wilson', None)
115+
INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?)
116+
INFO Engine [cached since 0.0008968s ago] ('Spider-Boy', 'Pedro Parqueador', None)
117+
INFO Engine INSERT INTO hero (name, secret_name, age) VALUES (?, ?, ?)
118+
INFO Engine [cached since 0.001143s ago] ('Rusty-Man', 'Tommy Sharp', 48)
119+
INFO Engine COMMIT
120+
```
121+
122+
17. Print the line `"After committing the session"`.
123+
124+
Generates the output:
125+
126+
```
127+
After committing the session
128+
```
129+
130+
18. Print the `hero_1` after committing the session.
131+
132+
The `hero_1` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data.
133+
134+
Generates the output:
135+
136+
```
137+
Hero 1:
138+
```
139+
140+
19. Print the `hero_2` after committing the session.
141+
142+
The `hero_2` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data.
143+
144+
Generates the output:
145+
146+
```
147+
Hero 2:
148+
```
149+
150+
20. Print the `hero_3` after committing the session.
151+
152+
The `hero_3` is now internally marked as expired, and until it is refreshed, it looks like if it didn't contain any data.
153+
154+
Generates the output:
155+
156+
```
157+
Hero 3:
158+
```
159+
160+
21. Print the line `"After commiting the session, show IDs"`.
161+
162+
Generates the output:
163+
164+
```
165+
After committing the session, show IDs
166+
```
167+
168+
22. Print the `hero_1.id`. A lot happens here.
169+
170+
Because we are accessing the attribute `id` of `hero_1`, **SQLModel** (actually SQLAlchemy) can detect that we are trying to access data from the `hero_1`.
171+
172+
It then detects that `hero_1` is currently associated with a **session** (because we added it to the session and committed it), and it is marked as expired.
173+
174+
Then with the **session**, it uses the **engine** to execute all the SQL to fetch the data for this object from the database.
175+
176+
Next it updates the object with the new data and marks it internally as "fresh" or "not expired".
177+
178+
Finally, it makes the ID value available for the rest of the Python expression. In this case, the Python expression just prints the ID.
179+
180+
Generates the output:
181+
182+
```
183+
INFO Engine BEGIN (implicit)
184+
INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age
185+
FROM hero
186+
WHERE hero.id = ?
187+
INFO Engine [generated in 0.00017s] (1,)
188+
189+
Hero 1 ID: 1
190+
```
191+
192+
23. Print the `hero_2.id`.
193+
194+
A lot happens here, all the same stuff that happened at point 22, but for this `hero_2` object.
195+
196+
Generates the output:
197+
198+
```
199+
INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age
200+
FROM hero
201+
WHERE hero.id = ?
202+
INFO Engine [cached since 0.001245s ago] (2,)
203+
204+
Hero 2 ID: 2
205+
```
206+
207+
24. Print the `hero_3.id`.
208+
209+
A lot happens here, all the same stuff that happened at point 22, but for this `hero_3` object.
210+
211+
Generates the output:
212+
213+
```
214+
INFO Engine SELECT hero.id AS hero_id, hero.name AS hero_name, hero.secret_name AS hero_secret_name, hero.age AS hero_age
215+
FROM hero
216+
WHERE hero.id = ?
217+
INFO Engine [cached since 0.002215s ago] (3,)
218+
219+
220+
Hero 3 ID: 3
221+
```
222+
223+
25. Print the line `"After committing the session, show names"`.
224+
225+
Generates the output:
226+
227+
```
228+
After committing the session, show names
229+
```
230+
231+
26. Print the `hero_1.name`.
232+
233+
Because `hero_1` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available.
234+
235+
Generates the output:
236+
237+
```
238+
Hero 1 name: Deadpond
239+
```
240+
241+
27. Print the `hero_2.name`.
242+
243+
Because `hero_2` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available.
244+
245+
Generates the output:
246+
247+
```
248+
Hero 2 name: Spider-Boy
249+
```
250+
251+
28. Print the `hero_3.name`.
252+
253+
Because `hero_3` is still fresh, no additional data is fetched, no additional SQL is executed, and the name is available.
254+
255+
Generates the output:
256+
257+
```
258+
Hero 3 name: Rusty-Man
259+
```
260+
261+
29. Explicitly refresh the `hero_1` object.
262+
263+
The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_1` object.
264+
265+
Generates the output:
266+
267+
```
268+
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
269+
FROM hero
270+
WHERE hero.id = ?
271+
INFO Engine [generated in 0.00024s] (1,)
272+
```
273+
274+
30. Explicitly refresh the `hero_2` object.
275+
276+
The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_2` object.
277+
278+
Generates the output:
279+
280+
```
281+
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
282+
FROM hero
283+
WHERE hero.id = ?
284+
INFO Engine [cached since 0.001487s ago] (2,)
285+
```
286+
287+
31. Explicitly refresh the `hero_3` object.
288+
289+
The **session** will use the **engine** to execute the SQL necessary to fetch fresh data from the database for the `hero_3` object.
290+
291+
Generates the output:
292+
293+
```
294+
INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age
295+
FROM hero
296+
WHERE hero.id = ?
297+
INFO Engine [cached since 0.002377s ago] (3,)
298+
```
299+
300+
32. Print the line `"After refreshing the heroes"`.
301+
302+
Generates the output:
303+
304+
```
305+
After refreshing the heroes
306+
```
307+
308+
33. Print the `hero_1`.
309+
310+
!!! info
311+
Even if the `hero_1` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute.
312+
313+
Because the `hero_1` is fresh it has all it's data available.
314+
315+
Generates the output:
316+
317+
```
318+
Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson'
319+
```
320+
321+
34. Print the `hero_2`.
322+
323+
!!! info
324+
Even if the `hero_2` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute.
325+
326+
Because the `hero_2` is fresh it has all it's data available.
327+
328+
Generates the output:
329+
330+
```
331+
Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador'
332+
```
333+
334+
35. Print the `hero_3`.
335+
336+
!!! info
337+
Even if the `hero_3` wasn't fresh, this would **not** trigger a `refresh` making the **session** use the **engine** to fetch data from the database because it is not accessing an attribute.
338+
339+
Because the `hero_3` is fresh it has all it's data available.
340+
341+
Generates the output:
342+
343+
```
344+
Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp'
345+
```
346+
347+
36. The `with` block ends here (there's no more indented code), so the **session** is closed, running all it's closing code.
348+
349+
This includes doing a `ROLLBACK` of any possible transaction that could have been started.
350+
351+
Generates the output:
352+
353+
```
354+
INFO Engine ROLLBACK
355+
```
356+
357+
37. Print the line `"After the session closes"`.
358+
359+
Generates the output:
360+
361+
```
362+
After the session closes
363+
```
364+
365+
38. Print the `hero_1` after closing the session.
366+
367+
Generates the output:
368+
369+
```
370+
Hero 1: age=None id=1 name='Deadpond' secret_name='Dive Wilson'
371+
```
372+
373+
39. Print the `hero_2` after closing the session.
374+
375+
Generates the output:
376+
377+
```
378+
Hero 2: age=None id=2 name='Spider-Boy' secret_name='Pedro Parqueador'
379+
```
380+
381+
40. Print the `hero_3` after closing the session.
382+
383+
Generates the output:
384+
385+
```
386+
Hero 3: age=48 id=3 name='Rusty-Man' secret_name='Tommy Sharp'
387+
```

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL