diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-08 20:58:05 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-08 20:58:05 -0400 |
commit | c14a3651a4ea624e0539c4ee7d45e23bf6139403 (patch) | |
tree | b990928374e5934af0c0a59c87cb1a5ed0cddbd4 /examples/inheritance/polymorph.py | |
parent | 0daf6e174d596e14e34f098f14d298a04a1b4d9b (diff) | |
download | sqlalchemy-c14a3651a4ea624e0539c4ee7d45e23bf6139403.tar.gz |
- modernizing examples
Diffstat (limited to 'examples/inheritance/polymorph.py')
-rw-r--r-- | examples/inheritance/polymorph.py | 78 |
1 files changed, 47 insertions, 31 deletions
diff --git a/examples/inheritance/polymorph.py b/examples/inheritance/polymorph.py index 7ca207f9f..872318060 100644 --- a/examples/inheritance/polymorph.py +++ b/examples/inheritance/polymorph.py @@ -1,19 +1,20 @@ -from sqlalchemy import MetaData, Table, Column, Integer, String, ForeignKey -from sqlalchemy.orm import mapper, relationship, create_session -import sets +from sqlalchemy import MetaData, Table, Column, Integer, String, \ + ForeignKey, create_engine +from sqlalchemy.orm import mapper, relationship, sessionmaker + # this example illustrates a polymorphic load of two classes -metadata = MetaData('sqlite://') -metadata.bind.echo = True +metadata = MetaData() # a table to store companies companies = Table('companies', metadata, Column('company_id', Integer, primary_key=True), Column('name', String(50))) -# we will define an inheritance relationship between the table "people" and "engineers", -# and a second inheritance relationship between the table "people" and "managers" +# we will define an inheritance relationship between the table "people" and +# "engineers", and a second inheritance relationship between the table +# "people" and "managers" people = Table('people', metadata, Column('person_id', Integer, primary_key=True), Column('company_id', Integer, ForeignKey('companies.company_id')), @@ -21,20 +22,20 @@ people = Table('people', metadata, Column('type', String(30))) engineers = Table('engineers', metadata, - Column('person_id', Integer, ForeignKey('people.person_id'), primary_key=True), + Column('person_id', Integer, ForeignKey('people.person_id'), + primary_key=True), Column('status', String(30)), Column('engineer_name', String(50)), Column('primary_language', String(50)), ) managers = Table('managers', metadata, - Column('person_id', Integer, ForeignKey('people.person_id'), primary_key=True), + Column('person_id', Integer, ForeignKey('people.person_id'), + primary_key=True), Column('status', String(30)), Column('manager_name', String(50)) ) -metadata.create_all() - # create our classes. The Engineer and Manager classes extend from Person. class Person(object): def __init__(self, **kwargs): @@ -44,10 +45,14 @@ class Person(object): return "Ordinary person %s" % self.name class Engineer(Person): def __repr__(self): - return "Engineer %s, status %s, engineer_name %s, primary_language %s" % (self.name, self.status, self.engineer_name, self.primary_language) + return "Engineer %s, status %s, engineer_name %s, "\ + "primary_language %s" % \ + (self.name, self.status, + self.engineer_name, self.primary_language) class Manager(Person): def __repr__(self): - return "Manager %s, status %s, manager_name %s" % (self.name, self.status, self.manager_name) + return "Manager %s, status %s, manager_name %s" % \ + (self.name, self.status, self.manager_name) class Company(object): def __init__(self, **kwargs): for key, value in kwargs.iteritems(): @@ -56,30 +61,43 @@ class Company(object): return "Company %s" % self.name -person_mapper = mapper(Person, people, polymorphic_on=people.c.type, polymorphic_identity='person') -mapper(Engineer, engineers, inherits=person_mapper, polymorphic_identity='engineer') -mapper(Manager, managers, inherits=person_mapper, polymorphic_identity='manager') +person_mapper = mapper(Person, people, polymorphic_on=people.c.type, + polymorphic_identity='person') +mapper(Engineer, engineers, inherits=person_mapper, + polymorphic_identity='engineer') +mapper(Manager, managers, inherits=person_mapper, + polymorphic_identity='manager') + +mapper(Company, companies, properties={'employees' + : relationship(Person, lazy='joined', backref='company', + cascade='all, delete-orphan')}) + +engine = create_engine('sqlite://', echo=True) + +metadata.create_all(engine) -mapper(Company, companies, properties={ - 'employees': relationship(Person, lazy='joined', backref='company', cascade="all, delete-orphan") -}) +session = sessionmaker(engine)() -session = create_session() c = Company(name='company1') -c.employees.append(Manager(name='pointy haired boss', status='AAB', manager_name='manager1')) -c.employees.append(Engineer(name='dilbert', status='BBA', engineer_name='engineer1', primary_language='java')) +c.employees.append(Manager(name='pointy haired boss', status='AAB', + manager_name='manager1')) +c.employees.append(Engineer(name='dilbert', status='BBA', + engineer_name='engineer1', primary_language='java')) c.employees.append(Person(name='joesmith', status='HHH')) -c.employees.append(Engineer(name='wally', status='CGG', engineer_name='engineer2', primary_language='python')) -c.employees.append(Manager(name='jsmith', status='ABA', manager_name='manager2')) +c.employees.append(Engineer(name='wally', status='CGG', + engineer_name='engineer2', primary_language='python' + )) +c.employees.append(Manager(name='jsmith', status='ABA', + manager_name='manager2')) session.add(c) -session.flush() -session.expunge_all() +session.commit() c = session.query(Company).get(1) for e in c.employees: print e, e._sa_instance_state.key, e.company -assert sets.Set([e.name for e in c.employees]) == sets.Set(['pointy haired boss', 'dilbert', 'joesmith', 'wally', 'jsmith']) +assert set([e.name for e in c.employees]) == set(['pointy haired boss', + 'dilbert', 'joesmith', 'wally', 'jsmith']) print "\n" dilbert = session.query(Person).filter_by(name='dilbert').one() @@ -88,14 +106,12 @@ assert dilbert is dilbert2 dilbert.engineer_name = 'hes dibert!' -session.flush() -session.expunge_all() +session.commit() c = session.query(Company).get(1) for e in c.employees: print e session.delete(c) -session.flush() +session.commit() -metadata.drop_all() |