On Feb 7, 2014, at 1:00 PM, Matt Phipps <matt.the.m...@gmail.com> wrote:

> 
> I wrote one more test that failed (but I'm pretty sure it doesn't matter): I 
> was under the impression that passing Label objects to .columns() would allow 
> you to map arbitrary result set column names to ORM attributes, and that 
> seems to not be the case (and was never the case, AFAIK). That kind of 
> mapping would be cool, and might not even be that hard since the columns in 
> the RowProxy ._keymap values seem to have the original ORM columns in their 
> .proxy_sets.

yeah I thought this would work but it requires a proxy_set change, which I’d 
like to make but has me nervous:

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    data = Column(String)

result = sess.query(A).from_statement(
                text("SELECT id AS x, data AS y FROM a").
                    columns(A.id.label("x"), A.data.label("y"))
                ).all()

I’ve added two different patches to 
http://www.sqlalchemy.org/trac/ticket/2932#comment:5 which is reopened.    both 
patches work but i think the second one is more of the right idea.

it works like this too but this renders the subquery, something else to think 
about maybe:

A1 = aliased(text("SELECT id AS x, data AS y FROM a").columns(A.id.label("x"), 
A.data.label("y")))

result = sess.query(A1).all()

as does this:

stmt = text("SELECT id AS x, data AS y FROM a").columns(A.id.label("x"), 
A.data.label("y"))

result = sess.query(A).select_entity_from(stmt).all()


> That said, the only reason I can think of for someone to try that is if they 
> did something truly nuts like a join with two columns with the same name from 
> two tables which also have the same name, from two different schemas, with a 
> stored procedure, into ORM.

well I really hate enforced naming conventions so making this work would be a 
breakthrough way of finally getting over that, I like it.  I think this can be 
done.

also, the change greatly increases performance as the lookup in ResultProxy 
doesn’t need a KeyError now.   So I really want to try to make it work.  I’m 
just trying to think of, what are the implications if the text() is then 
transformed into an alias() and such, but I think it might be consistent with 
how a Table acts right now.   I think its cool:

stmt = select([A.id, A.data])
result = sess.query(A).from_statement(stmt).all()   # works

stmt = select([A.id, A.data]).alias().select()
result = sess.query(A).from_statement(stmt).all() # you get the same column 
error


> I hope I'm not harassing you too much about the TextAsFrom feature! I feel 
> like if I asked any other ORM to be this flexible they would either laugh or 
> cry. SQLAlchemy is the first ORM I've worked with since using Rails as an 
> intern, and I'm spoiled now with how awesome it is :)

its great, this feature is going to be much better and important than how it 
started a few months ago.  I’ve added a lot of new thoughts to that ticket.


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to