Skip to content

Commit 53809d1

Browse files
committed
Update code example to Python 3.9 syntax
1 parent ec46cdf commit 53809d1

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

docs/advanced/advanced-relationships/self-referential.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To allow more fine-grained control over it, the `Relationship` constructor allow
1616

1717
Since SQLAlchemy relationships provide the [`remote_side`](https://docs.sqlalchemy.org/en/20/orm/relationship_api.html#sqlalchemy.orm.relationship.params.remote_side){.external-link target=_blank} parameter for just such an occasion, we can leverage that directly to construct the self-referential pattern with minimal code.
1818

19-
{* ./docs_src/advanced/self_referential/tutorial001.py ln[6:18] hl[16] *}
19+
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[6:18] hl[16] *}
2020

2121
Using the `sa_relationship_kwargs` parameter, we pass the keyword argument `remote_side='Villain.id'` to the underlying relationship property.
2222

@@ -46,17 +46,17 @@ Finally, as with regular (i.e. non-self-referential) foreign key relationships,
4646

4747
Now let's see how we can create villains with a boss:
4848

49-
{* ./docs_src/advanced/self_referential/tutorial001.py ln[31:50] hl[34:35] *}
49+
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:50] hl[34:35] *}
5050

5151
Just as with regular relationships, we can simply pass our boss villain as an argument to the constructor using `boss=thinnus`.
5252

5353
If we later learn that a villain actually had a secret boss after we've already created him, we can just as easily assign that boss retroactively:
5454

55-
{* ./docs_src/advanced/self_referential/tutorial001.py ln[31:32,52:56] hl[52] *}
55+
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:32,52:56] hl[52] *}
5656

5757
And if we want to add minions to a boss afterward, it's as easy as adding items to a Python list (because that's all it is 🤓):
5858

59-
{* ./docs_src/advanced/self_referential/tutorial001.py ln[31:32,58:69] hl[61] *}
59+
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:32,58:69] hl[61] *}
6060

6161
Since our relationships work both ways, we don't even need to add all our `clone_bot_`s to the session individually. Instead, we can simply add `ultra_bot` again and commit the changes. We do need to refresh them individually, though, if we want to access their updated attributes.
6262

docs_src/advanced/self_referential/tutorial001.py renamed to docs_src/advanced/self_referential/tutorial001_py39.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import Optional
22

33
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine
44

@@ -15,7 +15,7 @@ class Villain(SQLModel, table=True):
1515
back_populates="minions",
1616
sa_relationship_kwargs={"remote_side": "Villain.id"},
1717
)
18-
minions: List["Villain"] = Relationship(back_populates="boss")
18+
minions: list["Villain"] = Relationship(back_populates="boss")
1919

2020

2121
sqlite_file_name = "database.db"

tests/test_advanced/test_self_referential/test_tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282

8383
def test_tutorial(clear_sqlmodel):
84-
from docs_src.advanced.self_referential import tutorial001 as mod
84+
from docs_src.advanced.self_referential import tutorial001_py39 as mod
8585

8686
mod.sqlite_url = "sqlite://"
8787
mod.engine = create_engine(mod.sqlite_url)

0 commit comments

Comments
 (0)