Anders Langworthy wrote:
> Hi all,
>
> I've been trying to get the "grandchildren" relationship working in
> the code below, but have just about run out of permutations to try. I
> couldn't get much out of the docs, and the archives turned up a few
> similar queries but none with an answer. Is this even possible? Is
> there a better way to accomplish this result (the collection of
> related Grandchild objects accessible from an instance of Parent)?
>
> class Parent(Base):
> __tablename__ = 'parents'
>
> id = Column(Integer, primary_key=True)
>
> # this works of course
> children = relationship('Child', backref='parent')
>
> # but this doesn't
> grandchildren = relationship("Grandchild",
> primaryjoin="and_(Grandchild.childs_id==Child.id,\
> Child.parents_id==Parent.id)",
> foreign_keys=['Grandchild.childs_id','Child.parents_id'])
The error here regarding foreign keys is described here:
http://www.sqlalchemy.org/trac/wiki/FAQ#ImusingDeclarativeandsettingprimaryjoinsecondaryjoinusinganand_oror_andIamgettinganerrormessageaboutforeignkeys.
>
> class Child(Base):
> __tablename__ = 'childs'
>
> id = Column(Integer, primary_key=True)
> parents_id = Column(Integer, ForeignKey("parents.id"))
>
> class Grandchild(Base):
> __tablename__ = 'grandchilds'
>
> id = Column(Integer, primary_key=True)
> childs_id = Column(Integer, ForeignKey("childs.id"))
>
> This throws an exception:
> InvalidRequestError: One or more mappers failed to compile. Exception
> was probably suppressed within a hasattr() call. Message was: Could
> not determine relationship direction for primaryjoin condition
> 'grandchilds.childs_id = childs.id AND childs.parents_id =
> parents.id', on relationship Parent.grandchildren. Specify the
> 'foreign_keys' argument to indicate which columns on the relationship
> are foreign.
>
> Thanks for any help,
> Anders
>
> --
> 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.
>
>
--
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.