diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-07-28 17:05:50 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-07-28 17:05:50 -0400 |
| commit | 22ba1c43b792953ae6f791512d276739c8c09eae (patch) | |
| tree | bdf9f639b01426a8a2e1c8c61d35533026dd4265 /examples/generic_associations | |
| parent | 27913554a85c308d81e6c018669d0246ceecc639 (diff) | |
| download | sqlalchemy-22ba1c43b792953ae6f791512d276739c8c09eae.tar.gz | |
-whitespace bonanza, contd
Diffstat (limited to 'examples/generic_associations')
4 files changed, 41 insertions, 41 deletions
diff --git a/examples/generic_associations/__init__.py b/examples/generic_associations/__init__.py index b166d9161..36d50266e 100644 --- a/examples/generic_associations/__init__.py +++ b/examples/generic_associations/__init__.py @@ -1,8 +1,8 @@ """ -Illustrates various methods of associating multiple types of +Illustrates various methods of associating multiple types of parents with a particular child object. -The examples all use the declarative extension along with +The examples all use the declarative extension along with declarative mixins. Each one presents the identical use case at the end - two classes, ``Customer`` and ``Supplier``, both subclassing the ``HasAddresses`` mixin, which ensures that the diff --git a/examples/generic_associations/discriminator_on_association.py b/examples/generic_associations/discriminator_on_association.py index a73b4df1d..3c170d5c8 100644 --- a/examples/generic_associations/discriminator_on_association.py +++ b/examples/generic_associations/discriminator_on_association.py @@ -12,8 +12,8 @@ that refers to a particular table is present, the extra association table is used so that traditional foreign key constraints may be used. This configuration has the advantage that a fixed set of tables -are used, with no extra-table-per-parent needed. The individual -Address record can also locate its parent with no need to scan +are used, with no extra-table-per-parent needed. The individual +Address record can also locate its parent with no need to scan amongst many tables. """ @@ -26,7 +26,7 @@ from sqlalchemy.ext.associationproxy import association_proxy class Base(object): """Base class which provides automated table name and surrogate primary key column. - + """ @declared_attr def __tablename__(cls): @@ -37,17 +37,17 @@ Base = declarative_base(cls=Base) class AddressAssociation(Base): """Associates a collection of Address objects with a particular parent. - + """ __tablename__ = "address_association" @classmethod def creator(cls, discriminator): - """Provide a 'creator' function to use with + """Provide a 'creator' function to use with the association proxy.""" return lambda addresses:AddressAssociation( - addresses=addresses, + addresses=addresses, discriminator=discriminator) discriminator = Column(String) @@ -59,37 +59,37 @@ class AddressAssociation(Base): return getattr(self, "%s_parent" % self.discriminator) class Address(Base): - """The Address class. - - This represents all address records in a + """The Address class. + + This represents all address records in a single table. - + """ - association_id = Column(Integer, + association_id = Column(Integer, ForeignKey("address_association.id") ) street = Column(String) city = Column(String) zip = Column(String) association = relationship( - "AddressAssociation", + "AddressAssociation", backref="addresses") parent = association_proxy("association", "parent") def __repr__(self): return "%s(street=%r, city=%r, zip=%r)" % \ - (self.__class__.__name__, self.street, + (self.__class__.__name__, self.street, self.city, self.zip) class HasAddresses(object): """HasAddresses mixin, creates a relationship to the address_association table for each parent. - + """ @declared_attr def address_association_id(cls): - return Column(Integer, + return Column(Integer, ForeignKey("address_association.id")) @declared_attr @@ -99,8 +99,8 @@ class HasAddresses(object): "address_association", "addresses", creator=AddressAssociation.creator(discriminator) ) - return relationship("AddressAssociation", - backref=backref("%s_parent" % discriminator, + return relationship("AddressAssociation", + backref=backref("%s_parent" % discriminator, uselist=False)) @@ -117,7 +117,7 @@ session = Session(engine) session.add_all([ Customer( - name='customer 1', + name='customer 1', addresses=[ Address( street='123 anywhere street', diff --git a/examples/generic_associations/table_per_association.py b/examples/generic_associations/table_per_association.py index 86ee212dc..e1ff2be5b 100644 --- a/examples/generic_associations/table_per_association.py +++ b/examples/generic_associations/table_per_association.py @@ -6,7 +6,7 @@ for all parents. This configuration has the advantage that all Address rows are in one table, so that the definition of "Address" -can be maintained in one place. The association table +can be maintained in one place. The association table contains the foreign key to Address so that Address has no dependency on the system. @@ -20,7 +20,7 @@ from sqlalchemy.orm import Session, relationship class Base(object): """Base class which provides automated table name and surrogate primary key column. - + """ @declared_attr def __tablename__(cls): @@ -29,11 +29,11 @@ class Base(object): Base = declarative_base(cls=Base) class Address(Base): - """The Address class. - - This represents all address records in a + """The Address class. + + This represents all address records in a single table. - + """ street = Column(String) city = Column(String) @@ -41,23 +41,23 @@ class Address(Base): def __repr__(self): return "%s(street=%r, city=%r, zip=%r)" % \ - (self.__class__.__name__, self.street, + (self.__class__.__name__, self.street, self.city, self.zip) class HasAddresses(object): """HasAddresses mixin, creates a new address_association table for each parent. - + """ @declared_attr def addresses(cls): address_association = Table( "%s_addresses" % cls.__tablename__, cls.metadata, - Column("address_id", ForeignKey("address.id"), + Column("address_id", ForeignKey("address.id"), primary_key=True), - Column("%s_id" % cls.__tablename__, - ForeignKey("%s.id" % cls.__tablename__), + Column("%s_id" % cls.__tablename__, + ForeignKey("%s.id" % cls.__tablename__), primary_key=True), ) return relationship(Address, secondary=address_association) @@ -75,7 +75,7 @@ session = Session(engine) session.add_all([ Customer( - name='customer 1', + name='customer 1', addresses=[ Address( street='123 anywhere street', diff --git a/examples/generic_associations/table_per_related.py b/examples/generic_associations/table_per_related.py index 3130960b0..693908189 100644 --- a/examples/generic_associations/table_per_related.py +++ b/examples/generic_associations/table_per_related.py @@ -17,7 +17,7 @@ from sqlalchemy.orm import Session, relationship class Base(object): """Base class which provides automated table name and surrogate primary key column. - + """ @declared_attr def __tablename__(cls): @@ -26,13 +26,13 @@ class Base(object): Base = declarative_base(cls=Base) class Address(object): - """Define columns that will be present in each + """Define columns that will be present in each 'Address' table. - + This is a declarative mixin, so additional mapped attributes beyond simple columns specified here should be set up using @declared_attr. - + """ street = Column(String) city = Column(String) @@ -40,13 +40,13 @@ class Address(object): def __repr__(self): return "%s(street=%r, city=%r, zip=%r)" % \ - (self.__class__.__name__, self.street, + (self.__class__.__name__, self.street, self.city, self.zip) class HasAddresses(object): """HasAddresses mixin, creates a new Address class for each parent. - + """ @declared_attr def addresses(cls): @@ -54,9 +54,9 @@ class HasAddresses(object): "%sAddress" % cls.__name__, (Address, Base,), dict( - __tablename__ = "%s_address" % + __tablename__ = "%s_address" % cls.__tablename__, - parent_id = Column(Integer, + parent_id = Column(Integer, ForeignKey("%s.id" % cls.__tablename__)), parent = relationship(cls) ) @@ -76,7 +76,7 @@ session = Session(engine) session.add_all([ Customer( - name='customer 1', + name='customer 1', addresses=[ Customer.Address( street='123 anywhere street', |
