Skip to content

Commit bf737bf

Browse files
committed
Docs: multiple relationships to to the same model-fix examples, add retreiveal of foreign info example
1 parent 10b79b9 commit bf737bf

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

docs/tutorial/relationship-attributes/multiple-relationships-same-model.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Query Heros filtering by Team attributes by manually specifying the `join` with
128128
```Python hl_lines="7"
129129
# Code above omitted 👆
130130

131-
{!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:70-87]!}
131+
{!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:70-89]!}
132132

133133
# Code below omitted 👇
134134
```
@@ -155,7 +155,7 @@ From a query perspecitve, this is a much simpler solution. We use the `has` fun
155155
```Python hl_lines="5"
156156
# Code above omitted 👆
157157

158-
{!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:90-113]!}
158+
{!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:91-119]!}
159159

160160
# Code below omitted 👇
161161
```

docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ class Hero(SQLModel, table=True):
2828

2929
sqlite_file_name = ":memory:"
3030
sqlite_url = f"sqlite:///{sqlite_file_name}"
31-
mysql_url = "mysql+pymysql://[email protected]/test"
3231

33-
engine = create_engine(mysql_url, echo=True)
32+
engine = create_engine(sqlite_url, echo=True)
3433

3534

3635
def create_db_and_tables():
@@ -85,7 +84,11 @@ def select_heroes():
8584
8685
"""
8786
heros = result.all()
88-
print("Heros with Preventers as their winter team:", heros)
87+
print("Heros with Preventers as their winter team:")
88+
for hero in heros:
89+
print(
90+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
91+
)
8992

9093
# Heros with Preventers as their winter team and Z-Force as their summer team using "has" function.
9194
result = session.exec(
@@ -113,8 +116,11 @@ def select_heroes():
113116
heros = result.all()
114117
print(
115118
"Heros with Preventers as their winter and Z-Force as their summer team:",
116-
heros,
117119
)
120+
for hero in heros:
121+
print(
122+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
123+
)
118124
assert heros[0].name == "Deadpond"
119125

120126

docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001_py310.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ def select_heroes():
8181
WHERE team_1.name = ?
8282
8383
"""
84-
8584
heros = result.all()
86-
print("Heros with Preventers as their winter team:", heros)
87-
assert len(heros) == 2
85+
print("Heros with Preventers as their winter team:")
86+
for hero in heros:
87+
print(
88+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
89+
)
8890

89-
# Heros with Preventers as their winter team and Z-Force as their summer team
90-
# using "has" function.
91+
# Heros with Preventers as their winter team and Z-Force as their summer team using "has" function.
9192
result = session.exec(
9293
select(Hero)
9394
.where(Hero.winter_team.has(Team.name == "Preventers"))
@@ -113,9 +114,11 @@ def select_heroes():
113114
heros = result.all()
114115
print(
115116
"Heros with Preventers as their winter and Z-Force as their summer team:",
116-
heros,
117117
)
118-
assert len(heros) == 1
118+
for hero in heros:
119+
print(
120+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
121+
)
119122
assert heros[0].name == "Deadpond"
120123

121124

docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001_py39.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ def select_heroes():
8383
WHERE team_1.name = ?
8484
8585
"""
86-
8786
heros = result.all()
88-
print("Heros with Preventers as their winter team:", heros)
89-
assert len(heros) == 2
87+
print("Heros with Preventers as their winter team:")
88+
for hero in heros:
89+
print(
90+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
91+
)
9092

91-
# Heros with Preventers as their winter team and Z-Force as their summer team
92-
# using "has" function.
93+
# Heros with Preventers as their winter team and Z-Force as their summer team using "has" function.
9394
result = session.exec(
9495
select(Hero)
9596
.where(Hero.winter_team.has(Team.name == "Preventers"))
@@ -115,9 +116,11 @@ def select_heroes():
115116
heros = result.all()
116117
print(
117118
"Heros with Preventers as their winter and Z-Force as their summer team:",
118-
heros,
119119
)
120-
assert len(heros) == 1
120+
for hero in heros:
121+
print(
122+
f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}"
123+
)
121124
assert heros[0].name == "Deadpond"
122125

123126

0 commit comments

Comments
 (0)