Excerpts from gsandorx's message of Tue Dec 22 16:03:44 -0300 2009: > Hi Mariano, I tried your code and it worked great. Now I have to look > a way of linking the "union" query with a mapper to obtain a rather > elaborate object than a raw tuple, I mean, when you execute: > q = session.query(Computer).all() > you get Computer objects, but when you execute the "union" query you > get tuples.
Although they look like tuples, they're actually RowProxies and they provide some useful methods for solving this kind of situations. For more on RowProxy, please see [1] > When looking at the resulting tuples, I noticed that those tuples that > represent Computer objects contains a field equal to "None" due to the > extra column added in the query. I guess that field could be used > somehow to distinguish between Computer-like tuples and > CompSetComputer-like ones. I used a null value just a convenience to show the possible solution. Actually you could use whatever you want in there: the bottom problem was that you cannot union two queries with different quantities of columns. > I'm telling you this b/c I would like to > know if there is a mechanism to link a method to a mapper. This way, > the method would process the "union" query, and would create a list of > Computer and CompSetComputer objects. Do I make myself clear? > Thanks for your help, > Sandor > Off the top of my head, I could suggest a factory to build the objects. This example, of course, has nothing to do with the ORM. Maybe more tech savy guys can suggest better options. For example: class UnionComputers(object): """You can extend this class with whatever methods you would like""" def __init__(self, **kwargs): for every in kwargs.items(): setattr(self, every[0], every[1]) class UnionFactory(object): """Factory is maybe too big of a name since we're building just one type of object""" @staticmethod def return_inst(l_inst): for every in l_inst: yield UnionComputers(**dict([(str(x[0]), x[1]) for x in every.items()])) factory = UnionFactory() gen = factory.return_inst(session.execute(q3).fetchall()) while 1: v = gen.next() print(type(v), v.ip) [1] http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/connections.html#sqlalchemy.engine.base.RowProxy -- 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.