Skip to content

Commit 8124b83

Browse files
committed
includes for docs/tutorial/connect/read-connected-data.md
1 parent 893f8bd commit 8124b83

File tree

1 file changed

+7
-321
lines changed

1 file changed

+7
-321
lines changed

docs/tutorial/connect/read-connected-data.md

Lines changed: 7 additions & 321 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,6 @@ And the `hero` table has this data:
3535

3636
We will continue with the code in the previous example and we will add more things to it.
3737

38-
/// details | 👀 Full file preview
39-
40-
//// tab | Python 3.10+
41-
42-
```Python
43-
{!./docs_src/tutorial/connect/insert/tutorial001_py310.py!}
44-
```
45-
46-
////
47-
48-
//// tab | Python 3.7+
49-
50-
```Python
51-
{!./docs_src/tutorial/connect/insert/tutorial001.py!}
52-
```
53-
54-
////
55-
56-
///
57-
5838
## `SELECT` Connected Data with SQL
5939

6040
Let's start seeing how SQL works when selecting connected data. This is where SQL databases actually shine.
@@ -135,49 +115,7 @@ Remember SQLModel's `select()` function? It can take more than one argument.
135115

136116
So, we can pass the `Hero` and `Team` model classes. And we can also use both their columns in the `.where()` part:
137117

138-
//// tab | Python 3.10+
139-
140-
```Python hl_lines="5"
141-
# Code above omitted 👆
142-
143-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:61-63]!}
144-
145-
# Code below omitted 👇
146-
```
147-
148-
////
149-
150-
//// tab | Python 3.7+
151-
152-
```Python hl_lines="5"
153-
# Code above omitted 👆
154-
155-
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:63-65]!}
156-
157-
# Code below omitted 👇
158-
```
159-
160-
////
161-
162-
/// details | 👀 Full file preview
163-
164-
//// tab | Python 3.10+
165-
166-
```Python
167-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
168-
```
169-
170-
////
171-
172-
//// tab | Python 3.7+
173-
174-
```Python
175-
{!./docs_src/tutorial/connect/select/tutorial001.py!}
176-
```
177-
178-
////
179-
180-
///
118+
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:63] hl[5] *}
181119

182120
Notice that in the comparison with `==` we are using the class attributes for both `Hero.team_id` and `Team.id`.
183121

@@ -187,49 +125,7 @@ Now we can execute it and get the `results` object.
187125

188126
And as we used `select` with two models, we will receive tuples of instances of those two models, so we can iterate over them naturally in a `for` loop:
189127

190-
//// tab | Python 3.10+
191-
192-
```Python hl_lines="7"
193-
# Code above omitted 👆
194-
195-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:61-66]!}
196-
197-
# Code below omitted 👇
198-
```
199-
200-
////
201-
202-
//// tab | Python 3.7+
203-
204-
```Python hl_lines="7"
205-
# Code above omitted 👆
206-
207-
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:63-68]!}
208-
209-
# Code below omitted 👇
210-
```
211-
212-
////
213-
214-
/// details | 👀 Full file preview
215-
216-
//// tab | Python 3.10+
217-
218-
```Python
219-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
220-
```
221-
222-
////
223-
224-
//// tab | Python 3.7+
225-
226-
```Python
227-
{!./docs_src/tutorial/connect/select/tutorial001.py!}
228-
```
229-
230-
////
231-
232-
///
128+
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[61:66] hl[7] *}
233129

234130
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`.
235131

@@ -247,49 +143,7 @@ And you should get autocompletion and inline errors in your editor for both `her
247143

248144
As always, we must remember to add this new `select_heroes()` function to the `main()` function to make sure it is executed when we call this program from the command line.
249145

250-
//// tab | Python 3.10+
251-
252-
```Python hl_lines="6"
253-
# Code above omitted 👆
254-
255-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py[ln:69-72]!}
256-
257-
# Code below omitted 👇
258-
```
259-
260-
////
261-
262-
//// tab | Python 3.7+
263-
264-
```Python hl_lines="6"
265-
# Code above omitted 👆
266-
267-
{!./docs_src/tutorial/connect/select/tutorial001.py[ln:71-74]!}
268-
269-
# Code below omitted 👇
270-
```
271-
272-
////
273-
274-
/// details | 👀 Full file preview
275-
276-
//// tab | Python 3.10+
277-
278-
```Python
279-
{!./docs_src/tutorial/connect/select/tutorial001_py310.py!}
280-
```
281-
282-
////
283-
284-
//// tab | Python 3.7+
285-
286-
```Python
287-
{!./docs_src/tutorial/connect/select/tutorial001.py!}
288-
```
289-
290-
////
291-
292-
///
146+
{* ./docs_src/tutorial/connect/select/tutorial001_py310.py ln[69:72] hl[6] *}
293147

294148

295149
## Run the Program
@@ -396,49 +250,7 @@ The same way there's a `.where()` available when using `select()`, there's also
396250

397251
And in SQLModel (actually SQLAlchemy), when using the `.join()`, because we already declared what is the `foreign_key` when creating the models, we don't have to pass an `ON` part, it is inferred automatically:
398252

399-
//// tab | Python 3.10+
400-
401-
```Python hl_lines="5"
402-
# Code above omitted 👆
403-
404-
{!./docs_src/tutorial/connect/select/tutorial002_py310.py[ln:61-66]!}
405-
406-
# Code below omitted 👇
407-
```
408-
409-
////
410-
411-
//// tab | Python 3.7+
412-
413-
```Python hl_lines="5"
414-
# Code above omitted 👆
415-
416-
{!./docs_src/tutorial/connect/select/tutorial002.py[ln:63-68]!}
417-
418-
# Code below omitted 👇
419-
```
420-
421-
////
422-
423-
/// details | 👀 Full file preview
424-
425-
//// tab | Python 3.10+
426-
427-
```Python
428-
{!./docs_src/tutorial/connect/select/tutorial002_py310.py!}
429-
```
430-
431-
////
432-
433-
//// tab | Python 3.7+
434-
435-
```Python
436-
{!./docs_src/tutorial/connect/select/tutorial002.py!}
437-
```
438-
439-
////
440-
441-
///
253+
{* ./docs_src/tutorial/connect/select/tutorial002_py310.py ln[61:66] hl[5] *}
442254

443255
Also notice that we are still including `Team` in the `select(Hero, Team)`, because we still want to access that data.
444256

@@ -565,49 +377,7 @@ Now let's replicate the same query in **SQLModel**.
565377

566378
`.join()` has a parameter we can use `isouter=True` to make the `JOIN` be a `LEFT OUTER JOIN`:
567379

568-
//// tab | Python 3.10+
569-
570-
```Python hl_lines="5"
571-
# Code above omitted 👆
572-
573-
{!./docs_src/tutorial/connect/select/tutorial003_py310.py[ln:61-66]!}
574-
575-
# Code below omitted 👇
576-
```
577-
578-
////
579-
580-
//// tab | Python 3.7+
581-
582-
```Python hl_lines="5"
583-
# Code above omitted 👆
584-
585-
{!./docs_src/tutorial/connect/select/tutorial003.py[ln:63-68]!}
586-
587-
# Code below omitted 👇
588-
```
589-
590-
////
591-
592-
/// details | 👀 Full file preview
593-
594-
//// tab | Python 3.10+
595-
596-
```Python
597-
{!./docs_src/tutorial/connect/select/tutorial003_py310.py!}
598-
```
599-
600-
////
601-
602-
//// tab | Python 3.7+
603-
604-
```Python
605-
{!./docs_src/tutorial/connect/select/tutorial003.py!}
606-
```
607-
608-
////
609-
610-
///
380+
{* ./docs_src/tutorial/connect/select/tutorial003_py310.py ln[61:66] hl[5] *}
611381

612382
And if we run it, it will output:
613383

@@ -654,49 +424,7 @@ But we would still be able to **filter** the rows with it. 🤓
654424

655425
We could even add some additional `.where()` after `.join()` to filter the data more, for example to return only the heroes from one team:
656426

657-
//// tab | Python 3.10+
658-
659-
```Python hl_lines="5"
660-
# Code above omitted 👆
661-
662-
{!./docs_src/tutorial/connect/select/tutorial004_py310.py[ln:61-66]!}
663-
664-
# Code below omitted 👇
665-
```
666-
667-
////
668-
669-
//// tab | Python 3.7+
670-
671-
```Python hl_lines="5"
672-
# Code above omitted 👆
673-
674-
{!./docs_src/tutorial/connect/select/tutorial004.py[ln:63-68]!}
675-
676-
# Code below omitted 👇
677-
```
678-
679-
////
680-
681-
/// details | 👀 Full file preview
682-
683-
//// tab | Python 3.10+
684-
685-
```Python
686-
{!./docs_src/tutorial/connect/select/tutorial004_py310.py!}
687-
```
688-
689-
////
690-
691-
//// tab | Python 3.7+
692-
693-
```Python
694-
{!./docs_src/tutorial/connect/select/tutorial004.py!}
695-
```
696-
697-
////
698-
699-
///
427+
{* ./docs_src/tutorial/connect/select/tutorial004_py310.py ln[61:66] hl[5] *}
700428

701429
Here we are **filtering** with `.where()` to get only the heroes that belong to the **Preventers** team.
702430

@@ -727,49 +455,7 @@ Preventer Hero: id=2 secret_name='Tommy Sharp' team_id=1 name='Rusty-Man' age=48
727455

728456
By putting the `Team` in `select()` we tell **SQLModel** and the database that we want the team data too.
729457

730-
//// tab | Python 3.10+
731-
732-
```Python hl_lines="5"
733-
# Code above omitted 👆
734-
735-
{!./docs_src/tutorial/connect/select/tutorial005_py310.py[ln:61-66]!}
736-
737-
# Code below omitted 👇
738-
```
739-
740-
////
741-
742-
//// tab | Python 3.7+
743-
744-
```Python hl_lines="5"
745-
# Code above omitted 👆
746-
747-
{!./docs_src/tutorial/connect/select/tutorial005.py[ln:63-68]!}
748-
749-
# Code below omitted 👇
750-
```
751-
752-
////
753-
754-
/// details | 👀 Full file preview
755-
756-
//// tab | Python 3.10+
757-
758-
```Python
759-
{!./docs_src/tutorial/connect/select/tutorial005_py310.py!}
760-
```
761-
762-
////
763-
764-
//// tab | Python 3.7+
765-
766-
```Python
767-
{!./docs_src/tutorial/connect/select/tutorial005.py!}
768-
```
769-
770-
////
771-
772-
///
458+
{* ./docs_src/tutorial/connect/select/tutorial005_py310.py ln[61:66] hl[5] *}
773459

774460
And if we run that, it will output:
775461

0 commit comments

Comments
 (0)