diff options
Diffstat (limited to 'examples/inheritance')
-rw-r--r-- | examples/inheritance/concrete.py | 26 | ||||
-rw-r--r-- | examples/inheritance/polymorph.py | 78 | ||||
-rw-r--r-- | examples/inheritance/single.py | 56 |
3 files changed, 98 insertions, 62 deletions
diff --git a/examples/inheritance/concrete.py b/examples/inheritance/concrete.py index c50513b55..eb832a55d 100644 --- a/examples/inheritance/concrete.py +++ b/examples/inheritance/concrete.py @@ -1,5 +1,6 @@ -from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String -from sqlalchemy.orm import mapper, create_session, polymorphic_union +from sqlalchemy import create_engine, MetaData, Table, Column, Integer, \ + String +from sqlalchemy.orm import mapper, sessionmaker, polymorphic_union metadata = MetaData() @@ -30,14 +31,16 @@ class Manager(Employee): self.name = name self.manager_data = manager_data def __repr__(self): - return self.__class__.__name__ + " " + self.name + " " + self.manager_data + return self.__class__.__name__ + " " + \ + self.name + " " + self.manager_data class Engineer(Employee): def __init__(self, name, engineer_info): self.name = name self.engineer_info = engineer_info def __repr__(self): - return self.__class__.__name__ + " " + self.name + " " + self.engineer_info + return self.__class__.__name__ + " " + \ + self.name + " " + self.engineer_info pjoin = polymorphic_union({ @@ -46,11 +49,15 @@ pjoin = polymorphic_union({ }, 'type', 'pjoin') employee_mapper = mapper(Employee, pjoin, polymorphic_on=pjoin.c.type) -manager_mapper = mapper(Manager, managers_table, inherits=employee_mapper, concrete=True, polymorphic_identity='manager') -engineer_mapper = mapper(Engineer, engineers_table, inherits=employee_mapper, concrete=True, polymorphic_identity='engineer') +manager_mapper = mapper(Manager, managers_table, + inherits=employee_mapper, concrete=True, + polymorphic_identity='manager') +engineer_mapper = mapper(Engineer, engineers_table, + inherits=employee_mapper, concrete=True, + polymorphic_identity='engineer') -session = create_session(bind=engine) +session = sessionmaker(engine)() m1 = Manager("pointy haired boss", "manager1") e1 = Engineer("wally", "engineer1") @@ -59,8 +66,7 @@ e2 = Engineer("dilbert", "engineer2") session.add(m1) session.add(e1) session.add(e2) -session.flush() +session.commit() -employees = session.query(Employee) -print [e for e in employees] +print session.query(Employee).all() 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() diff --git a/examples/inheritance/single.py b/examples/inheritance/single.py index 29b214b20..a7883fcbf 100644 --- a/examples/inheritance/single.py +++ b/examples/inheritance/single.py @@ -1,8 +1,8 @@ -from sqlalchemy import MetaData, Table, Column, Integer, String, ForeignKey -from sqlalchemy.orm import mapper, relationship, create_session +from sqlalchemy import MetaData, Table, Column, Integer, String, \ + ForeignKey, create_engine +from sqlalchemy.orm import mapper, relationship, sessionmaker -metadata = MetaData('sqlite://') -metadata.bind.echo = 'debug' +metadata = MetaData() # a table to store companies companies = Table('companies', metadata, @@ -20,7 +20,6 @@ employees_table = Table('employees', metadata, Column('manager_name', String(50)) ) -metadata.create_all() class Person(object): def __init__(self, **kwargs): @@ -30,10 +29,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(): @@ -41,25 +44,38 @@ class Company(object): def __repr__(self): return "Company %s" % self.name -person_mapper = mapper(Person, employees_table, polymorphic_on=employees_table.c.type, polymorphic_identity='person') -manager_mapper = mapper(Manager, inherits=person_mapper, polymorphic_identity='manager') -engineer_mapper = mapper(Engineer, inherits=person_mapper, polymorphic_identity='engineer') +person_mapper = mapper(Person, employees_table, + polymorphic_on=employees_table.c.type, + polymorphic_identity='person') +manager_mapper = mapper(Manager, inherits=person_mapper, + polymorphic_identity='manager') +engineer_mapper = mapper(Engineer, inherits=person_mapper, + polymorphic_identity='engineer') mapper(Company, companies, properties={ 'employees': relationship(Person, lazy=True, backref='company') }) -session = create_session() + +engine = create_engine('sqlite:///', echo=True) + +metadata.create_all(engine) + +session = sessionmaker(engine)() + 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: @@ -81,6 +97,4 @@ for e in c.employees: print e session.delete(c) -session.flush() - -metadata.drop_all() +session.commit() |