On Dec 2, 2010, at 5:30 AM, Андрей wrote: > Hello. > There is an example (found in > http://discorporate.us/jek/talks/pgwest08-sqlalchemy.pdf) > >>>> sql = r''' > ... SELECT * > ... FROM ticket JOIN ticket_change > ... ON (ticket.id = ticket_change.ticket) > ... WHERE summary ~ '[[:digit:]]+' > ... ORDER BY ticket.time desc > ... LIMIT 1 > ... ''' >>>> session.query(Ticket, Change).from_statement(sql).first() > Traceback (most recent call last): > ... > InvalidRequestError: Ambiguous column name 'time' > in result set! try 'use_labels' option on select statement. > > And as its solution provides the following example: > >>>> q3 = session.query(Ticket, Change) \ > .select_from(tickets.join(ticket_changes)) \ > .filter(Ticket.id==1) \ > .order_by(desc(Change.time)) >>>> print q3 > SELECT ticket.id AS ticket_id, ... > ticket_change.ticket AS ticket_change_ticket, ... > FROM ticket JOIN ticket_change > ON ticket.id = ticket_change.ticket > WHERE ticket.id = %(id_1)s > ORDER BY ticket_change.time DESC >>>> q3.first() > (Ticket(1, 'document arguments for table, column, ...'), > Change(1, 1149120761, 'status', 'closed')) > > So can we all still use from_statement for raw queries where there are > joining exists. > Maybe there are some ways to add some labels in query ?
when from_statement() is used, columns can be in one of two forms: select a, b from table or select a as table_a, b as table_b from table where the latter version disambiguates column names based on their table or alias of origin. If you have a select() construct, calling "s = s.apply_labels()" will produce this effect for the select() construct itself. > > -- > 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. > -- 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.