> -----Original Message----- > From: sqlalchemy@googlegroups.com > [mailto:sqlalch...@googlegroups.com] On Behalf Of Michael Bayer > Sent: 27 January 2010 16:31 > To: sqlalchemy@googlegroups.com > Subject: Re: [sqlalchemy] Map to Arbitrary Select Using Raw SQL > > Michael Chambliss wrote: > > Hey Michael - thanks for the patient and helpful response. I played > > around with the from_statement() approach earlier today, > but what I was > > able to derive seemed to follow the standard model of define table, > > define class, map table to class, execute query. That > approach would be > > great assuming I can map to some composite result (IE, > > multi-table/function). Perhaps I need to dive further into this to > > determine how joins are handled and how the mapping should > be defined > > for them. The original example I linked seemed to imply > some mapping > > magic in that the "Customer" class wasn't defined but was > mapped to the > > complex "Selectable". However, my research and attempts to do this > > mapping with from_statement() proved fruitless. > > > > In a theoretical example, say I have a CAR table that > refers to both a > > CAR_TYPE table and CAR_ATTRIBUTES table. CAR_TYPE is simply an > > enumeration for a static list of types, and CAR_ATTRIBUTES is an > > arbitrarily long list of key,value attributes (color, > weight, top speed, > > etc). So, ultimately, a "Car" is made up of these three. > > > > I'd want to bake all of these together, passing in a CAR.ID (primary > > key) to map to a Car instance. I prefer to live in SQL because I'm > > pretty good at it, and I need to reference, specifically, > Oracle Spatial > > and Workspace functions. I do not, however, need to chain > additional > > filters off of this, handle updates/inserts (at least at > this point), > > etc. I'm literally just looking for a "cheap" way to map a > row to an > > object and scoop up connection pooling, type handling, and > other great > > things I'll probably learn about as I go. > > > Assuming you've configured Car, CarType and CarAttributes > with mappers,
My reading of the original email is that configuring the mapping is the problem that the OP is having. To the OP: As far as I'm aware, you can't configure a mapper directly against a textual SQL statement. However, you might be able to get away with using the declarative syntax to define your Car class: http://www.sqlalchemy.org/docs/reference/ext/declarative.html#synopsis You'll have to give it a table name, which will have the effect of defining a Table object even though no such table exists in the database, but I don't think this matters. Then you could use the query that Mike suggested to actually retrieve rows. Hope that helps, Simon -- 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.