summaryrefslogtreecommitdiff
path: root/examples/poly_assoc/poly_assoc.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/poly_assoc/poly_assoc.py')
-rw-r--r--examples/poly_assoc/poly_assoc.py48
1 files changed, 28 insertions, 20 deletions
diff --git a/examples/poly_assoc/poly_assoc.py b/examples/poly_assoc/poly_assoc.py
index ecdc403e7..1854bfa3d 100644
--- a/examples/poly_assoc/poly_assoc.py
+++ b/examples/poly_assoc/poly_assoc.py
@@ -1,28 +1,29 @@
"""
"polymorphic" associations, ala ActiveRecord.
-In this example, we are specifically targeting this ActiveRecord functionality:
+In this example, we are specifically targeting this ActiveRecord
+functionality:
http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations
-The term "polymorphic" here means "object X can be referenced by objects A, B, and C,
-along a common line of association".
+The term "polymorphic" here means "object X can be referenced by objects A, B,
+and C, along a common line of association".
-In this example we illustrate the relationship in both directions.
-A little bit of property magic is used to smooth the edges.
+In this example we illustrate the relationship in both directions. A little
+bit of property magic is used to smooth the edges.
-AR creates this relationship in such a way that disallows
-any foreign key constraint from existing on the association.
-For a different way of doing this, see
-poly_assoc_fks.py. The interface is the same, the efficiency is more or less the same,
-but foreign key constraints may be used. That example also better separates
-the associated target object from those which associate with it.
+AR creates this relationship in such a way that disallows any foreign key
+constraint from existing on the association. For a different way of doing
+this, see poly_assoc_fks.py. The interface is the same, the efficiency is more
+or less the same, but foreign key constraints may be used. That example also
+better separates the associated target object from those which associate with
+it.
"""
from sqlalchemy import MetaData, Table, Column, Integer, String, and_
-from sqlalchemy.orm import (mapper, relationship, create_session, class_mapper,
- backref)
+from sqlalchemy.orm import mapper, relationship, sessionmaker, \
+ class_mapper, backref
metadata = MetaData('sqlite://')
@@ -41,12 +42,15 @@ addresses = Table("addresses", metadata,
class Address(object):
def __init__(self, type):
self.addressable_type = type
- member = property(lambda self: getattr(self, '_backref_%s' % self.addressable_type))
+ @property
+ def member(self):
+ return getattr(self, '_backref_%s' % self.addressable_type)
def addressable(cls, name, uselist=True):
"""addressable 'interface'.
- if you really wanted to make a "generic" version of this function, it's straightforward.
+ if you really wanted to make a "generic" version of this function, it's
+ straightforward.
"""
# create_address function, imitaes the rails example.
@@ -71,8 +75,13 @@ def addressable(cls, name, uselist=True):
foreign_keys = [addresses.c.addressable_id]
mapper.add_property(name, relationship(
Address,
- primaryjoin=primaryjoin, uselist=uselist, foreign_keys=foreign_keys,
- backref=backref('_backref_%s' % table.name, primaryjoin=list(table.primary_key)[0] == addresses.c.addressable_id, foreign_keys=foreign_keys)
+ primaryjoin=primaryjoin,
+ uselist=uselist,
+ foreign_keys=foreign_keys,
+ backref=backref('_backref_%s' % table.name,
+ primaryjoin=list(table.primary_key)[0] ==\
+ addresses.c.addressable_id,
+ foreign_keys=foreign_keys)
)
)
@@ -124,12 +133,11 @@ a2.street = '345 orchard ave'
a3 = o1.create_address()
a3.street = '444 park ave.'
-sess = create_session()
+sess = sessionmaker()()
sess.add(u1)
sess.add(o1)
-sess.flush()
-sess.expunge_all()
+sess.commit()
# query objects, get their addresses