-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomap.py
More file actions
27 lines (20 loc) · 813 Bytes
/
Automap.py
File metadata and controls
27 lines (20 loc) · 813 Bytes
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
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
Base = automap_base()
# engine, suppose it has two tables 'user' and 'address' set up
engine = create_engine("mysql+pymysql://root@localhost:3306/test1to1")
# reflect the tables
Base.prepare(engine, reflect=True)
# mapped classes are now created with names by default
# matching that of the table name.
Parent = Base.classes.parent
Child = Base.classes.child
session = Session(engine)
# rudimentary relationships are produced
session.add(Child(name="annyu", parent=Parent(name="fatw")))
session.commit()
instance1 = session.query(Parent).filter(Parent.name == 'hyyu').one()
# collection-based relationships are by default named
# "<classname>_collection"
#print (u1.address_collection)