-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_setup.py
32 lines (31 loc) · 904 Bytes
/
data_setup.py
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
from sqlalchemy import Column, create_engine, ForeignKey, insert, Integer, MetaData, Table
engine = create_engine("sqlite+pysqlite:///:memory:", echo=True)
def setup_data(engine=engine):
metadata_object = MetaData()
table_a = Table(
"table_a",
metadata_object,
Column("id", Integer, primary_key=True),
)
table_b = Table(
"table_b",
metadata_object,
Column("id", Integer, primary_key=True),
Column("a_id", ForeignKey("table_a.id"), nullable=False),
)
metadata_object.create_all(engine)
with engine.connect() as conn:
conn.execute(
insert(table_a),
[
{"id": 1},
]
)
conn.execute(
insert(table_b),
[
{"id": 1, "a_id": 1},
{"id": 2, "a_id": 1},
]
)
conn.commit()