]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
update poly_assoc examples for 0.4+ syntax
authorGaëtan de Menten <gdementen@gmail.com>
Thu, 3 Jul 2008 13:19:31 +0000 (13:19 +0000)
committerGaëtan de Menten <gdementen@gmail.com>
Thu, 3 Jul 2008 13:19:31 +0000 (13:19 +0000)
examples/poly_assoc/poly_assoc.py
examples/poly_assoc/poly_assoc_fk.py
examples/poly_assoc/poly_assoc_generic.py

index c13ffbfa196f12a7245676f8904737a85baa4d56..3823f4b7f0e3856d2e28e4032fe292fd232a33ac 100644 (file)
@@ -6,14 +6,14 @@ 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".  
+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.
@@ -28,7 +28,7 @@ metadata = MetaData('sqlite://')
 #######
 # 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)),
@@ -44,10 +44,10 @@ class Address(object):
 
 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.
@@ -58,8 +58,8 @@ def addressable(cls, name, uselist=True):
         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.
@@ -73,18 +73,18 @@ def addressable(cls, name, uselist=True):
             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
 
@@ -94,10 +94,10 @@ addressable(User, 'addresses', uselist=True)
 ######
 # 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
 
@@ -132,16 +132,16 @@ sess.clear()
 
 # 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
 
 
-    
+
index 22ee50009a95997922b230bcbd5e36078d91c196..32600fcbf2a273e2f8aa8f486a732c1641abd820 100644 (file)
@@ -5,7 +5,7 @@ See "poly_assoc.py" for an imitation of this functionality as implemented
 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
@@ -15,7 +15,7 @@ flexibility in its usage.
 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.
 """
 
@@ -27,7 +27,7 @@ metadata = MetaData('sqlite://')
 #######
 # 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)),
@@ -36,7 +36,7 @@ addresses = Table("addresses", metadata,
     )
 
 ## 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)
 )
@@ -47,12 +47,12 @@ class Address(object):
 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
@@ -74,7 +74,7 @@ def addressable(cls, name, uselist=True):
                 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={
@@ -84,13 +84,13 @@ 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
 
@@ -100,13 +100,13 @@ addressable(User, 'addresses', uselist=True)
 ######
 # 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
 
@@ -145,13 +145,13 @@ sess.clear()
 
 # 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
index 4fca310193b2f1a1195a0f46a25a58973a27b122..8cc3edf60952610eccdb4b5b8367872078cc684f 100644 (file)
@@ -1,7 +1,7 @@
 """
 "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".
 """
@@ -13,20 +13,20 @@ metadata = MetaData('sqlite://')
 
 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)
@@ -49,20 +49,20 @@ def association(cls, table):
                     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)),
@@ -82,12 +82,12 @@ 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),
     Column('assoc_id', None, ForeignKey('addresses_associations.assoc_id'))
     )
-    
+
 class User(object):
     pass
 
@@ -99,12 +99,12 @@ addressable(User, 'addresses', uselist=True)
 ######
 # 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
 
@@ -141,13 +141,13 @@ sess.clear()
 
 # 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