summaryrefslogtreecommitdiff
path: root/examples/inheritance/single.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/single.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/single.py')
-rw-r--r--examples/inheritance/single.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/inheritance/single.py b/examples/inheritance/single.py
new file mode 100644
index 000000000..00feaefe8
--- /dev/null
+++ b/examples/inheritance/single.py
@@ -0,0 +1,86 @@
+from sqlalchemy import MetaData, Table, Column, Integer, String, ForeignKey
+from sqlalchemy.orm import mapper, relation, create_session
+
+metadata = MetaData('sqlite://')
+metadata.bind.echo = 'debug'
+
+# a table to store companies
+companies = Table('companies', metadata,
+ Column('company_id', Integer, primary_key=True),
+ Column('name', String(50)))
+
+employees_table = Table('employees', metadata,
+ Column('employee_id', Integer, primary_key=True),
+ Column('company_id', Integer, ForeignKey('companies.company_id')),
+ Column('name', String(50)),
+ Column('type', String(20)),
+ Column('status', String(20)),
+ Column('engineer_name', String(50)),
+ Column('primary_language', String(50)),
+ Column('manager_name', String(50))
+)
+
+metadata.create_all()
+
+class Person(object):
+ def __init__(self, **kwargs):
+ for key, value in kwargs.iteritems():
+ setattr(self, key, value)
+ def __repr__(self):
+ 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)
+class Manager(Person):
+ def __repr__(self):
+ 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():
+ setattr(self, key, value)
+ 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')
+
+mapper(Company, companies, properties={
+ 'employees': relation(Person, lazy=True, backref='company')
+})
+
+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(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'))
+session.add(c)
+session.flush()
+
+session.expunge_all()
+
+c = session.query(Company).get(1)
+for e in c.employees:
+ print e, e.company
+
+print "\n"
+
+dilbert = session.query(Person).filter_by(name='dilbert').one()
+dilbert2 = session.query(Engineer).filter_by(name='dilbert').one()
+assert dilbert is dilbert2
+
+dilbert.engineer_name = 'hes dibert!'
+
+session.flush()
+session.expunge_all()
+
+c = session.query(Company).get(1)
+for e in c.employees:
+ print e
+
+session.delete(c)
+session.flush()
+
+metadata.drop_all()