-
-
Notifications
You must be signed in to change notification settings - Fork 868
Expand file tree
/
Copy pathtest_annotated_relationship.py
More file actions
85 lines (69 loc) · 3.18 KB
/
Copy pathtest_annotated_relationship.py
File metadata and controls
85 lines (69 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Annotated
from sqlalchemy.orm import Mapped
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select
def test_annotated_relationship_with_default() -> None:
class Team(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
heroes: Annotated[list["Hero"], Relationship(back_populates="team")] = [] # noqa: RUF012
class Hero(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
team_id: Annotated[int | None, Field(foreign_key="team.id")] = None
team: Annotated[Team | None, Relationship(back_populates="heroes")] = None
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
team = Team(name="Preventers")
hero = Hero(name="Deadpond", team=team)
session.add(hero)
session.commit()
session.refresh(hero)
assert hero.team is not None
assert hero.team.name == "Preventers"
team_db = session.exec(select(Team)).one()
assert [h.name for h in team_db.heroes] == ["Deadpond"]
def test_annotated_relationship_without_default() -> None:
class Team(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
heroes: Annotated[list["Hero"], Relationship(back_populates="team")]
class Hero(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
team_id: Annotated[int | None, Field(foreign_key="team.id")] = None
team: Annotated[Team | None, Relationship(back_populates="heroes")]
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
team = Team(name="Z-Force")
hero = Hero(name="Spider-Boy", team=team)
session.add(hero)
session.commit()
session.refresh(hero)
assert hero.team is not None
assert hero.team.name == "Z-Force"
def test_annotated_mapped_relationship() -> None:
class Team(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
heroes: Annotated[
Mapped[list["Hero"]], Relationship(back_populates="team")
] = [] # noqa: RUF012
class Hero(SQLModel, table=True):
id: Annotated[int | None, Field(primary_key=True)] = None
name: Annotated[str, Field(index=True)]
team_id: Annotated[int | None, Field(foreign_key="team.id")] = None
team: Annotated[Mapped[Team | None], Relationship(back_populates="heroes")] = (
None
)
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
team = Team(name="Avengers")
hero = Hero(name="Iron Man", team=team)
session.add(hero)
session.commit()
session.refresh(hero)
assert hero.team is not None
assert hero.team.name == "Avengers"