1from sqlalchemy.ext.automap import automap_base
2from sqlalchemy.orm import Session
3from sqlalchemy import create_engine
4
5Base = automap_base()
6
7# engine, suppose it has two tables 'user' and 'address' set up
8engine = create_engine("sqlite:///mydatabase.db")
9
10# reflect the tables
11Base.prepare(engine, reflect=True)
12
13# mapped classes are now created with names by default
14# matching that of the table name.
15User = Base.classes.user
16Address = Base.classes.address
17
18session = Session(engine)
19
20# rudimentary relationships are produced
21session.add(Address(email_address="foo@bar.com", user=User(name="foo")))
22session.commit()
23
24# collection-based relationships are by default named
25# "<classname>_collection"
26print (u1.address_collection)
27