summaryrefslogtreecommitdiff
path: root/examples/inheritance/concrete.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-01-19 00:53:12 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2010-01-19 00:53:12 +0000
commit40f8aadd582776524d3b98da1f577c2fc95619e7 (patch)
tree753eec3802734f397953976824a252bb60829189 /examples/inheritance/concrete.py
parent56fe538cc7d81ce264fc6504feb1ead5e17d0f55 (diff)
downloadsqlalchemy-40f8aadd582776524d3b98da1f577c2fc95619e7.tar.gz
- mega example cleanup
- added READMEs to all examples in each __init__.py and added to sphinx documentation - added versioning example - removed vertical/vertical.py, the dictlikes are more straightforward
Diffstat (limited to 'examples/inheritance/concrete.py')
-rw-r--r--examples/inheritance/concrete.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/inheritance/concrete.py b/examples/inheritance/concrete.py
new file mode 100644
index 000000000..c50513b55
--- /dev/null
+++ b/examples/inheritance/concrete.py
@@ -0,0 +1,66 @@
+from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
+from sqlalchemy.orm import mapper, create_session, polymorphic_union
+
+metadata = MetaData()
+
+managers_table = Table('managers', metadata,
+ Column('employee_id', Integer, primary_key=True),
+ Column('name', String(50)),
+ Column('manager_data', String(40))
+)
+
+engineers_table = Table('engineers', metadata,
+ Column('employee_id', Integer, primary_key=True),
+ Column('name', String(50)),
+ Column('engineer_info', String(40))
+)
+
+engine = create_engine('sqlite:///', echo=True)
+metadata.create_all(engine)
+
+
+class Employee(object):
+ def __init__(self, name):
+ self.name = name
+ def __repr__(self):
+ return self.__class__.__name__ + " " + self.name
+
+class Manager(Employee):
+ def __init__(self, name, manager_data):
+ self.name = name
+ self.manager_data = manager_data
+ def __repr__(self):
+ 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
+
+
+pjoin = polymorphic_union({
+ 'manager':managers_table,
+ 'engineer':engineers_table
+}, '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')
+
+
+session = create_session(bind=engine)
+
+m1 = Manager("pointy haired boss", "manager1")
+e1 = Engineer("wally", "engineer1")
+e2 = Engineer("dilbert", "engineer2")
+
+session.add(m1)
+session.add(e1)
+session.add(e2)
+session.flush()
+
+employees = session.query(Employee)
+print [e for e in employees]
+