On Oct 25, 2011, at 7:50 AM, Jules Stevenson wrote: > Hi All, > > I'm trying to query more than one class at the same time, and order > the result by date (both classes have a .date property), using the > following query: > > c.items = meta.Session.query(model.ArkEmail, model.ArkNote).\ > filter(model.ArkEmail.is_thread_parent == True).\ > filter(model.ArkEmail.project == c.project).\ > filter(model.ArkEmail.is_duplicate == False).\ > filter(model.ArkNote.project.contains(c.project)).\ > order_by(sa.desc(model.ArkEmail.date), > sa.desc(model.ArkNote.date)).\ > all() > > There's not really any mapping between these two classes, I jsut want > a list ordered by date of the two class instances that match the > filter criteria. > > I'm hitting some wierdness
OK nothing should ever be "weird", you turn on SQL echoing, look at the SQL being emitted, and whatever is happening will be revealed. in this case, the glaring error is that you aren't associating ArkEmail and ArkNote together which will give you a cartesian product (wikipedia is calling it "implicit cross join": http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join there's likely some other glitches here causing you to get unexpected results, so watching the SQL would illustrate what is actually going on. The key to any query is understanding the SQL representation first - if you can't write the query in SQL then you won't be able to get SQLAlchemy to do it either. > > It's also returning the result inside a tuple: > >>>> c.items > [(<ark.model.clientprojectshot.ArkEmail object at 0x00000000073FF208>, > <ark.model.utilities.ArkNote object at 0x00000000073FF278>)] that is to be expected. Read up on how Query works at http://www.sqlalchemy.org/docs/orm/tutorial.html#querying . -- 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.