Created
December 18, 2013 16:09
-
-
Save zaccone/8024982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Association(Base): | |
__tablename__ = 'association' | |
left_id = Column(Integer, ForeignKey('left.id'), primary_key=True) | |
right_id = Column(Integer, ForeignKey('right.id'), primary_key=True) | |
extra_data = Column(String(50)) | |
child = relationship("Child", backref="parent_assocs") | |
class Parent(Base): | |
__tablename__ = 'left' | |
id = Column(Integer, primary_key=True) | |
children = relationship("Association", backref="parent") | |
class Child(Base): | |
__tablename__ = 'right' | |
id = Column(Integer, primary_key=True) | |
Working with the association pattern in its direct form requires that child objects are associated with an association instance before being appended to the parent; similarly, access from parent to child goes through the association object: | |
# create parent, append a child via association | |
p = Parent() | |
a = Association(extra_data="some data") | |
a.child = Child() | |
p.children.append(a) | |
# iterate through child objects via association, including association | |
# attributes | |
for assoc in p.children: | |
print assoc.extra_data | |
print assoc.child |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment