Commits

Anonymous committed 46e877b

update poly_assoc examples for 0.4+ syntax

  • Participants
  • Parent commits c23f755

Comments (0)

Files changed (3)

File examples/poly_assoc/poly_assoc.py

 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".  
+along a common line of association".
 
-In this example we illustrate the relationship in both directions.  
+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 
+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.
 #######
 # addresses table, class, 'addressable interface'.
 
-addresses = Table("addresses", metadata, 
+addresses = Table("addresses", metadata,
     Column('id', Integer, primary_key=True),
     Column('addressable_id', Integer),
     Column('addressable_type', String(50)),
 
 def addressable(cls, name, uselist=True):
     """addressable 'interface'.
-    
+
     if you really wanted to make a "generic" version of this function, it's straightforward.
     """
-    
+
     # create_address function, imitaes the rails example.
     # we could probably use property tricks as well to set
     # the Address object's "addressabletype" attribute.
         else:
             setattr(self, name, a)
         return a
-    
-    mapper = class_mapper(cls)    
+
+    mapper = class_mapper(cls)
     table = mapper.local_table
     cls.create_address = create_address
     # no constraints.  therefore define constraints in an ad-hoc fashion.
             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)
         )
-    )    
+    )
 
 mapper(Address, addresses)
 
 ######
 # sample # 1, users
 
-users = Table("users", metadata, 
+users = Table("users", metadata,
     Column('id', Integer, primary_key=True),
     Column('name', String(50), nullable=False)
     )
-    
+
 class User(object):
     pass
 
 ######
 # sample # 2, orders
 
-orders = Table("orders", metadata, 
+orders = Table("orders", metadata,
     Column('id', Integer, primary_key=True),
     Column('description', String(50), nullable=False))
-    
+
 class Order(object):
     pass
 
 
 # query objects, get their addresses
 
-bob = sess.query(User).get_by(name='bob')
+bob = sess.query(User).filter_by(name='bob').one()
 assert [s.street for s in bob.addresses] == ['123 anywhere street', '345 orchard ave']
 
-order = sess.query(Order).get_by(description='order 1')
+order = sess.query(Order).filter_by(description='order 1').one()
 assert order.address.street == '444 park ave.'
 
 # query from Address to members
 
-for address in sess.query(Address).list():
+for address in sess.query(Address).all():
     print "Street", address.street, "Member", address.member
 
 
-    
+

File examples/poly_assoc/poly_assoc_fk.py

 in ActiveRecord.
 
 Here, we build off the previous example, adding an association table
-that allows the relationship to be expressed as a many-to-one from the 
+that allows the relationship to be expressed as a many-to-one from the
 "model" object to its "association", so that each model table bears the foreign
 key constraint.  This allows the same functionality via traditional
 normalized form with full constraints.  It also isolates the target
 As in the previous example, a little bit of property magic is used
 to smooth the edges.
 
-For a more genericized version of this example, see 
+For a more genericized version of this example, see
 poly_assoc_generic.py.
 """
 
 #######
 # addresses table, class, 'addressable interface'.
 
-addresses = Table("addresses", metadata, 
+addresses = Table("addresses", metadata,
     Column('id', Integer, primary_key=True),
     Column('assoc_id', None, ForeignKey('address_associations.assoc_id')),
     Column('street', String(100)),
     )
 
 ## association table
-address_associations = Table("address_associations", metadata, 
+address_associations = Table("address_associations", metadata,
     Column('assoc_id', Integer, primary_key=True),
     Column('type', String(50), nullable=False)
 )
 class AddressAssoc(object):
     def __init__(self, name):
         self.type = name
-    
+
 def addressable(cls, name, uselist=True):
     """addressable 'interface'.
-    
-    we create this function here to imitate the style used in poly_assoc.py.  
-    
+
+    we create this function here to imitate the style used in poly_assoc.py.
+
     """
     mapper = class_mapper(cls)
     table = mapper.local_table
                 self.address_rel = AddressAssoc(table.name)
             self.address_rel.addresses = [value]
         setattr(cls, name, property(get, set))
-        
+
 mapper(Address, addresses)
 
 mapper(AddressAssoc, address_associations, properties={
 ######
 # sample # 1, users
 
-users = Table("users", metadata, 
+users = Table("users", metadata,
     Column('id', Integer, primary_key=True),
     Column('name', String(50), nullable=False),
     # this column ties the users table into the address association
     Column('assoc_id', None, ForeignKey('address_associations.assoc_id'))
     )
-    
+
 class User(object):
     pass
 
 ######
 # sample # 2, orders
 
-orders = Table("orders", metadata, 
+orders = Table("orders", metadata,
     Column('id', Integer, primary_key=True),
     Column('description', String(50), nullable=False),
     # this column ties the orders table into the address association
     Column('assoc_id', None, ForeignKey('address_associations.assoc_id'))
     )
-    
+
 class Order(object):
     pass
 
 
 # query objects, get their addresses
 
-bob = sess.query(User).get_by(name='bob')
+bob = sess.query(User).filter_by(name='bob').one()
 assert [s.street for s in bob.addresses] == ['123 anywhere street', '345 orchard ave']
 
-order = sess.query(Order).get_by(description='order 1')
+order = sess.query(Order).filter_by(description='order 1').one()
 assert order.address.street == '444 park ave.'
 
 # query from Address to members
 
-for address in sess.query(Address).list():
+for address in sess.query(Address).all():
     print "Street", address.street, "Member", address.member

File examples/poly_assoc/poly_assoc_generic.py

 """
 "polymorphic" associations, ala SQLAlchemy.
 
-This example generalizes the function in poly_assoc_pk.py into a 
+This example generalizes the function in poly_assoc_pk.py into a
 function "association" which creates a new polymorphic association
 "interface".
 """
 
 def association(cls, table):
     """create an association 'interface'."""
-    
+
     interface_name = table.name
     attr_name = "%s_rel" % interface_name
 
     metadata = table.metadata
-    association_table = Table("%s_associations" % interface_name, metadata, 
+    association_table = Table("%s_associations" % interface_name, metadata,
         Column('assoc_id', Integer, primary_key=True),
         Column('type', String(50), nullable=False)
     )
-    
+
     class GenericAssoc(object):
         def __init__(self, name):
             self.type = name
-    
+
     def interface(cls, name, uselist=True):
 
         mapper = class_mapper(cls)
                     setattr(self, attr_name, GenericAssoc(table.name))
                 getattr(self, attr_name).targets = [value]
             setattr(cls, name, property(get, set))
-        
+
     setattr(cls, 'member', property(lambda self: getattr(self.association, '_backref_%s' % self.association.type)))
-    
+
     mapper(GenericAssoc, association_table, properties={
         'targets':relation(cls, backref='association'),
     })
-    
+
     return interface
 
 
 #######
 # addresses table
 
-addresses = Table("addresses", metadata, 
+addresses = Table("addresses", metadata,
     Column('id', Integer, primary_key=True),
     Column('assoc_id', None, ForeignKey('addresses_associations.assoc_id')),
     Column('street', String(100)),
 ######
 # sample # 1, users
 
-users = Table("users", metadata, 
+users = Table("users", metadata,
     Column('id', Integer, primary_key=True),
     Column('name', String(50), nullable=False),
     Column('assoc_id', None, ForeignKey('addresses_associations.assoc_id'))
     )
-    
+
 class User(object):
     pass
 
 ######
 # sample # 2, orders
 
-orders = Table("orders", metadata, 
+orders = Table("orders", metadata,
     Column('id', Integer, primary_key=True),
     Column('description', String(50), nullable=False),
     Column('assoc_id', None, ForeignKey('addresses_associations.assoc_id'))
     )
-    
+
 class Order(object):
     pass
 
 
 # query objects, get their addresses
 
-bob = sess.query(User).get_by(name='bob')
+bob = sess.query(User).filter_by(name='bob').one()
 assert [s.street for s in bob.addresses] == ['123 anywhere street', '345 orchard ave']
 
-order = sess.query(Order).get_by(description='order 1')
+order = sess.query(Order).filter_by(description='order 1').one()
 assert order.address.street == '444 park ave.'
 
 # query from Address to members
 
-for address in sess.query(Address).list():
+for address in sess.query(Address).all():
     print "Street", address.street, "Member", address.member