Hi, Could anyone help point me in the right direction here? Any help would be much appreciated!
Thanks, Jesse On May 30, 12:54 pm, Jesse Cohen <ske...@gmail.com> wrote: > This is my first post here, I've been learning about the query api and I've > got three related questions: > > 1. Suppose I have a User model and UserThing which is loaded into the > User.things collection, UserThing is polymorphic via joined table > inheritance. I'd like to do something like this: > > subq = s.query(UserThing).with_polymorphic('*').with_labels().subquery() > users_with_eagerly_loaded_things = s.query(User).join(subq, > User.things).options(contains_eager(User.things, alias=subq)).all() > > This works, the query appears to be rendered correctly, and the base-class > attributes of the collection are populated eagerly and work fine, but the > polymorphic attributes (attributes unique to the inherited classes) of the > collections aren't populated, so when I do: > > print users_with_eagerly_loaded_things[0].some_polylmorphic_attribute > > Sqlalchemy issues more sql to fetch those attributes even though they were > already fetched in the subquery and should have been populated by the > contains_eager option. It is as if the subquery only queried the table for > the base class, even though I have verified that when I add the > with_polymorphic('*') it joins against all the inherited tables as well. > What am I doing wrong? In addition to doing this via a query, is there an > attribute I can specify on a collection to have that collection always use a > polymorphic eager load? > > 2. On a similar note, suppose if in that join I want to specify a join > condition manually, how do I refer to the alias of the subquery -- I cant > seem to figure out the syntax of that, e.g. > > s.query(User).join(subq, subq.user_id == User.id) #(but subq.user_id > gives me an error) > > 3. One final question, suppose I want an explicit join kind of like this > that returns tuples of (User, UserThing): > > s.query(User, UserThing).join(subq).all() > > but the above will give me a cartesian product of Users and UserThings, > because sqlalchemy doesn't know that the subquery is specifying UserThings. > What I really want is results returned as the subq joined to the user in a > tuple, this works > > s.query(User, subq).join(subq) > > but doesn't return results in the form I want. I get all of the fields of > the subquery individually rather than nicely being populated into a > UserThing object, e.g. results look like (User, userthing_id, > userthing_name, userthing_data). How do I get this query to return results > that look like (User,UserThing) if the UserThings are fetched via a > subquery? > > Finally, I also just wanted to say how awesome I find your software -- I > just started using sqlalchemy about a month ago and every time I need to do > something I am amazed by how sqlalchemy handles it and how much easier it > makes my life as well as the extensive support community which exists. > > Thanks!! > > Best, > Jesse Cohen -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.