Noufal wrote: > > Hello everyone, > I've been using sqlalchemy with elixir for a legacy project for a > while now and recently needed to write some more than trivial queries. > I have the default elixir generated mappers but using only them forces > me to do some data processing in my app rather than in the database. > This makes it slow. > > What I need to do is something like this. I have a table of clients > which contains id, name and a couple of other fields. This is linked > to an orders table through a OneToMany relationship (each client had > multiple orders). Every row of the order table contains some details > like date, type etc. and a backlink (foreign key) back to the clients > table. > > I need to query out a list of clients and their latest orders. My > mappers are not set up for this and so I need to do a manual query. > Can someone suggest how I'd do this in sqlalchemy?
finding a list of objects and the "most recent"/"highest"/"<something>est" related item requires joining to a subquery, where the subquery selects the func.max(desiredfield) and GROUP BY's the columns that relate the rows to the parent. An example of that is in the ORM tutorial under "Using Subqueries". Replace func.count() with func.max() and you're on your way. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---