I'm sorry if this is an obvious question, but there seem to be a
couple ways to do joins, and I seem unable to find an example
explaining how to achieve my particular result.

I need to join two tables, and I want the mapper objects of both
tables returned. Essentially, I want

    session.query(m1, m2)

but of course, that doesn't do an actual join, and I need to use
filter() instead of an onclause. While in this particular case I don't
really care, I would imagine that there probably are scenarios where
one would, so my first question is: Can the same result (multiple
mappers returned) be achieved while generating an actual sql JOIN
statement?

Now specifically, the query i am using is between three models, Sale,
Item, and an m2m SoldItem. I need to join all three tables, and want
to return Sale and SoldItem objects.

If I use this query:

     session.query(Sale, SoldItem)\
         .join((Item, SoldItem.item_id==Item.id)).\
         .filter(...sale/solditem condition...)

it actually generates invalid SQL (Unknown column 'solditem.item_id'
in 'on clause'"):

     SELECT ... FROM solditem, sale INNER JOIN item ON
solditem.item_id = item.id

That is, it tries to join "sale" with "item", which is incorrect.
Apparently, the tables in the from clause are listed reverse from how
I passed the classes to query(), so I actually have to use:

    session.query(SoldItem, Sale) ...

to make the query work, which is rather confusing. If this is how the
interaction between query() and join() is supposed to work, then
shouldn't the join() apply at least to the last mapper listed? Is this
a bug?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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.

Reply via email to