[Sqlalchemy-tickets] Issue #3501: full positional mapping w/ ORM / corresponding compat for TextAsF
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2015-07-26 22:33:55
|
New issue 3501: full positional mapping w/ ORM / corresponding compat for TextAsFrom https://bitbucket.org/zzzeek/sqlalchemy/issues/3501/full-positional-mapping-w-orm Mike Bayer: tons of use cases for TextAsFrom that should be intuitive that don't work. When we make a TextAsFrom with a positional set of columns, those columns should be welded to it. The statement should be able to work in any ORM context flawlessly, no reliance on names matching up should be needed as we do not target on name anymore: ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) bs = relationship("B") class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) a_id = Column(ForeignKey('a.id')) e = create_engine("sqlite://", echo='debug') Base.metadata.create_all(e) s = Session(e) s.add_all([ A(bs=[B(), B()]), A(bs=[B(), B()]) ]) s.commit() b1 = aliased(B) # works sql = "select a.id, ba.id as bid, ba.a_id from "\ "a left outer join b as ba on a.id=ba.a_id" # fails. why? # sql = "select a.id as aid, ba.id as bid, ba.a_id from "\ # "a left outer join b as ba on a.id=ba.a_id" # fails. why? # sql = "select a.id as aid, ba.id, ba.a_id from "\ # "a left outer join b as ba on a.id=ba.a_id" # are we relying upon names somehow? we should be able to # be 100% positional now t = text(sql).columns(A.id, b1.id, b1.a_id) q = s.query(A).from_statement(t).options(contains_eager(A.bs, alias=b1)) for a in q: print a.id print a, a.bs # forget about if we try a1 = aliased(A) also... ``` I've added docs in 7d268d4bcb5e6205d05ac and 4f51fa947ffa0cadeab7ad7dc that we may even have to dial back for versions that don't have this feature. |