Works like a charm! Thank you! (once again)
2011/1/3 Michael Bayer <[email protected]>: > I'd keep each path separate, i.e. > > query.options(*[joinedload_all(path) for path in relationshipsToPreLoad]) > > > On Jan 3, 2011, at 10:55 AM, Hector Blanco wrote: > >> Hi list! >> >> I am facing a little problem whose I'm sure has a very simple >> solution, but I haven't been able to find it (the solution, I mean)... >> >> I would like to be able to pass the fields (relationships) I want to >> pre-load as a parameter. Let's say I have a couple of classes: >> >> ------------------------------------------------- >> class ElementsGroup(declarativeBase): >> """Represents a group of elements""" >> __tablename__ = "elements_groups" >> >> _id = Column("id", Integer, primary_key=True) >> >> elements = relationship("Element" ... , collection_class=set ) >> elementGroups = relationship("ElementGroup", ... , >> collection_class=set) >> ------------------------------------------------- >> >> And then the "Element" class: >> >> ------------------------------------------------- >> class Element(declarativeBase): >> __tablename__ = "elements" >> >> _id = Column("id", Integer, primary_key=True) >> otherThings = relationship("Things", ... , collection_class=set) >> ------------------------------------------------- >> >> I would like to create a method to load objects of "ElementsGroups" >> that would accept a parameter containing which relationships have to >> be pre-loaded with a joinedload. Something like: >> >> ------------------------------------------------- >> class ElementsGroupManager(object): >> @staticmethod >> def getAll(relationshipsToPreLoad=list()): >> retval = list >> try: >> if (relationshipsToPreLoad): >> retval = >> Database.session.query(ElementsGroup.ElementsGroup).options(joinedload_all(relationshipsToPreLoad)).all() >> else: >> retval = >> Database.session.query(ElementsGroup.ElementsGroup).all() >> finally: >> Database.session.close() >> return retval >> ------------------------------------------------- >> >> And in relationshipsToPreload I can say, for instance: >> ["elements", "elements.otherThings"] >> (and would preload ElementsGroup.elements and the "otherThings" field >> of each "element" object in the ElementsGroup.elements >> list/relationship) >> or >> ["elementGroups"] >> which would just pre-load ElementsGroups.elementGroups (and not the >> ElementsGroups.elements) >> >> I'm sure it's very easy, but I haven't been able to do it... If I try >> to pass relationshipsToPreLoad=['elements', 'elementGroups'] I get >> something like: >> >>> Mapper 'Mapper|Element|elements' has no property 'elementGroups' >> >> It looks like it's trying to load (correctly) ElementsGroups.elements >> but then it's trying to load ElementsGroups.elements.elementGroups >> (which is not what I want... I want to load >> ElementsGroups.elementGroups) >> >> Thanks in advance! >> >> -- >> 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. > > -- 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.
