From:
http://www.sqlalchemy.org/docs/orm/relationships.html?highlight=backref#association-object
I'm able to get the association to work, and according to the docs,
adding the backref allows the query to be executed in reverse.
How do I get the Parent of Child #1? Obviously I could query Child,
but my template is set to output based on Parent. If I do the
following, I get:
AttributeError: Neither 'InstrumentedAttribute' object nor
'Comparator' object has an attribute 'id'
content = DBSession.query(Parent).filter(Child.id==1).all()
constructs the query incorrectly (well, incorrectly for what I'm
trying to accomplish)
SELECT "left".id AS left_id
FROM "left", "right"
WHERE "right".id = :id_1
code follows:
Base = declarative_base()
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)
child = relationship("Child", backref="parent_assocs")
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship(Association, backref="parent")
def __repr__(self):
return '<Parent: id: %d children %s>' % (self.id,
self.children)
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
def __repr__(self):
return '<Child: id: %d>' % self.id
content = DBSession.query(Parent).filter(Parent.children.id==1).all()
print content
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.