FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlmodel/docs_src/tutorial/connect/insert/tutorial001.py at main · codassassin/sqlmodel · GitHub
codassassin
sqlmodel
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlmodel
/
docs_src
/
tutorial
/
connect
/
insert
/
tutorial001.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
69 lines (49 loc) · 1.8 KB
Breadcrumbs
sqlmodel
/
docs_src
/
tutorial
/
connect
/
insert
/
tutorial001.py
Copy path
File metadata and controls
69 lines (49 loc) · 1.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
typing
import
Optional
from
sqlmodel
import
Field
,
Session
,
SQLModel
,
create_engine
class
Team
(
SQLModel
,
table
=
True
):
id
:
Optional
[
int
]
=
Field
(
default
=
None
,
primary_key
=
True
)
name
:
str
headquarters
:
str
class
Hero
(
SQLModel
,
table
=
True
):
id
:
Optional
[
int
]
=
Field
(
default
=
None
,
primary_key
=
True
)
name
:
str
secret_name
:
str
age
:
Optional
[
int
]
=
None
team_id
:
Optional
[
int
]
=
Field
(
default
=
None
,
foreign_key
=
"team.id"
)
sqlite_file_name
=
"database.db"
sqlite_url
=
f"sqlite:///
{
sqlite_file_name
}
"
engine
=
create_engine
(
sqlite_url
,
echo
=
True
)
def
create_db_and_tables
():
SQLModel
.
metadata
.
create_all
(
engine
)
def
create_heroes
():
with
Session
(
engine
)
as
session
:
team_preventers
=
Team
(
name
=
"Preventers"
,
headquarters
=
"Sharp Tower"
)
team_z_force
=
Team
(
name
=
"Z-Force"
,
headquarters
=
"Sister Margaret’s Bar"
)
session
.
add
(
team_preventers
)
session
.
add
(
team_z_force
)
session
.
commit
()
hero_deadpond
=
Hero
(
name
=
"Deadpond"
,
secret_name
=
"Dive Wilson"
,
team_id
=
team_z_force
.
id
)
hero_rusty_man
=
Hero
(
name
=
"Rusty-Man"
,
secret_name
=
"Tommy Sharp"
,
age
=
48
,
team_id
=
team_preventers
.
id
,
)
hero_spider_boy
=
Hero
(
name
=
"Spider-Boy"
,
secret_name
=
"Pedro Parqueador"
)
session
.
add
(
hero_deadpond
)
session
.
add
(
hero_rusty_man
)
session
.
add
(
hero_spider_boy
)
session
.
commit
()
session
.
refresh
(
hero_deadpond
)
session
.
refresh
(
hero_rusty_man
)
session
.
refresh
(
hero_spider_boy
)
print
(
"Created hero:"
,
hero_deadpond
)
print
(
"Created hero:"
,
hero_rusty_man
)
print
(
"Created hero:"
,
hero_spider_boy
)
def
main
():
create_db_and_tables
()
create_heroes
()
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL