Esempio n. 1
0
Session = sessionmaker(bind=engine)
work_session = Session()

### Ensure an empty database (so you can run this script more than once)
for cls in (Student, Course):
    for obj in work_session.query(cls).all():
        work_session.delete(obj)

### Build non-empty initial state
fred = Student(id=1, name="Fred Brooks")
diana = Student(id=4, name="Diana Shirley")

phy = Course(id=2, title="Phy 101")
cs = Course(id=3, title="CS Basics")

fred.courses = [cs]
diana.courses = [phy]

for it in (fred, diana, phy, cs):
    work_session.merge(it)

work_session.commit()
show(work_session, "init state")

### Open up a second session on the same engine
read_session = Session()

### Make some changes in the work_session
elaine = Student(id=5, name="Elanor Rigby")
math = Course(id=4, title="Calc 7", students=[fred, diana, elaine])