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

✨ Add source examples for docs · alejsdev/sqlmodel@e630814 · GitHub

Repository navigation

Commit e630814

Browse files
committed
✨ Add source examples for docs
1 parent e1cfd99 commit e630814

126 files changed

Lines changed: 5778 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs_src/__init__.py‎

Whitespace-only changes.

‎docs_src/tutorial/__init__.py‎

Whitespace-only changes.

‎docs_src/tutorial/automatic_id_none_refresh/__init__.py‎

Whitespace-only changes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, Session, SQLModel, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str
9+
secret_name: str
10+
age: Optional[int] = None
11+
12+
13+
sqlite_file_name = "database.db"
14+
sqlite_url = f"sqlite:///{sqlite_file_name}"
15+
16+
engine = create_engine(sqlite_url, echo=True)
17+
18+
19+
def create_db_and_tables():
20+
SQLModel.metadata.create_all(engine)
21+
22+
23+
def create_heroes():
24+
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
25+
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
26+
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
27+
28+
print("Before interacting with the database")
29+
print("Hero 1:", hero_1)
30+
print("Hero 2:", hero_2)
31+
print("Hero 3:", hero_3)
32+
33+
with Session(engine) as session:
34+
session.add(hero_1)
35+
session.add(hero_2)
36+
session.add(hero_3)
37+
38+
print("After adding to the session")
39+
print("Hero 1:", hero_1)
40+
print("Hero 2:", hero_2)
41+
print("Hero 3:", hero_3)
42+
43+
session.commit()
44+
45+
print("After committing the session")
46+
print("Hero 1:", hero_1)
47+
print("Hero 2:", hero_2)
48+
print("Hero 3:", hero_3)
49+
50+
print("After committing the session, show IDs")
51+
print("Hero 1 ID:", hero_1.id)
52+
print("Hero 2 ID:", hero_2.id)
53+
print("Hero 3 ID:", hero_3.id)
54+
55+
print("After committing the session, show names")
56+
print("Hero 1 name:", hero_1.name)
57+
print("Hero 2 name:", hero_2.name)
58+
print("Hero 3 name:", hero_3.name)
59+
60+
session.refresh(hero_1)
61+
session.refresh(hero_2)
62+
session.refresh(hero_3)
63+
64+
print("After refreshing the heroes")
65+
print("Hero 1:", hero_1)
66+
print("Hero 2:", hero_2)
67+
print("Hero 3:", hero_3)
68+
69+
print("After the session closes")
70+
print("Hero 1:", hero_1)
71+
print("Hero 2:", hero_2)
72+
print("Hero 3:", hero_3)
73+
74+
75+
def main():
76+
create_db_and_tables()
77+
create_heroes()
78+
79+
80+
if __name__ == "__main__":
81+
main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, Session, SQLModel, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str
9+
secret_name: str
10+
age: Optional[int] = None
11+
12+
13+
sqlite_file_name = "database.db"
14+
sqlite_url = f"sqlite:///{sqlite_file_name}"
15+
16+
engine = create_engine(sqlite_url, echo=True)
17+
18+
19+
def create_db_and_tables():
20+
SQLModel.metadata.create_all(engine)
21+
22+
23+
def create_heroes():
24+
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson") # (1)
25+
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador") # (2)
26+
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48) # (3)
27+
28+
print("Before interacting with the database") # (4)
29+
print("Hero 1:", hero_1) # (5)
30+
print("Hero 2:", hero_2) # (6)
31+
print("Hero 3:", hero_3) # (7)
32+
33+
with Session(engine) as session: # (8)
34+
session.add(hero_1) # (9)
35+
session.add(hero_2) # (10)
36+
session.add(hero_3) # (11)
37+
38+
print("After adding to the session") # (12)
39+
print("Hero 1:", hero_1) # (13)
40+
print("Hero 2:", hero_2) # (14)
41+
print("Hero 3:", hero_3) # (15)
42+
43+
session.commit() # (16)
44+
45+
print("After committing the session") # (17)
46+
print("Hero 1:", hero_1) # (18)
47+
print("Hero 2:", hero_2) # (19)
48+
print("Hero 3:", hero_3) # (20)
49+
50+
print("After committing the session, show IDs") # (21)
51+
print("Hero 1 ID:", hero_1.id) # (22)
52+
print("Hero 2 ID:", hero_2.id) # (23)
53+
print("Hero 3 ID:", hero_3.id) # (24)
54+
55+
print("After committing the session, show names") # (25)
56+
print("Hero 1 name:", hero_1.name) # (26)
57+
print("Hero 2 name:", hero_2.name) # (27)
58+
print("Hero 3 name:", hero_3.name) # (28)
59+
60+
session.refresh(hero_1) # (29)
61+
session.refresh(hero_2) # (30)
62+
session.refresh(hero_3) # (31)
63+
64+
print("After refreshing the heroes") # (32)
65+
print("Hero 1:", hero_1) # (33)
66+
print("Hero 2:", hero_2) # (34)
67+
print("Hero 3:", hero_3) # (35)
68+
# (36)
69+
70+
print("After the session closes") # (37)
71+
print("Hero 1:", hero_1) # (38)
72+
print("Hero 2:", hero_2) # (39)
73+
print("Hero 3:", hero_3) # (40)
74+
75+
76+
def main():
77+
create_db_and_tables()
78+
create_heroes()
79+
80+
81+
if __name__ == "__main__":
82+
main()

‎docs_src/tutorial/code_structure/__init__.py‎

Whitespace-only changes.

‎docs_src/tutorial/code_structure/tutorial001/__init__.py‎

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from sqlmodel import Session
2+
3+
from .database import create_db_and_tables, engine
4+
from .models import Hero, Team
5+
6+
7+
def create_heroes():
8+
with Session(engine) as session:
9+
team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")
10+
11+
hero_deadpond = Hero(
12+
name="Deadpond", secret_name="Dive Wilson", team=team_z_force
13+
)
14+
session.add(hero_deadpond)
15+
session.commit()
16+
17+
session.refresh(hero_deadpond)
18+
19+
print("Created hero:", hero_deadpond)
20+
print("Hero's team:", hero_deadpond.team)
21+
22+
23+
def main():
24+
create_db_and_tables()
25+
create_heroes()
26+
27+
28+
if __name__ == "__main__":
29+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from sqlmodel import SQLModel, create_engine
2+
3+
sqlite_file_name = "database.db"
4+
sqlite_url = f"sqlite:///{sqlite_file_name}"
5+
6+
engine = create_engine(sqlite_url)
7+
8+
9+
def create_db_and_tables():
10+
SQLModel.metadata.create_all(engine)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Optional
2+
3+
from sqlmodel import Field, Relationship, SQLModel
4+
5+
6+
class Team(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str
9+
headquarters: str
10+
11+
heroes: List["Hero"] = Relationship(back_populates="team")
12+
13+
14+
class Hero(SQLModel, table=True):
15+
id: Optional[int] = Field(default=None, primary_key=True)
16+
name: str
17+
secret_name: str
18+
age: Optional[int] = None
19+
20+
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
21+
team: Optional[Team] = Relationship(back_populates="heroes")

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL