transfer label names when adapting column selectables

Issue #1942 resolved
Mike Bayer repo owner created an issue

"major" because of the testing needed

diff -r ee9102622c8538bd7e2594b05dd8bdc72317d3d0 lib/sqlalchemy/orm/query.py
--- a/lib/sqlalchemy/orm/query.py   Sun Oct 03 13:11:41 2010 -0400
+++ b/lib/sqlalchemy/orm/query.py   Sat Oct 09 10:03:12 2010 -0400
@@ -2696,6 +2696,7 @@

     def adapt_to_selectable(self, query, sel):
         c = _ColumnEntity(query, sel.corresponding_column(self.column))
+        c._label_name = self._label_name
         c.entity_zero = self.entity_zero
         c.entities = self.entities

Comments (3)

  1. Mike Bayer reporter

    alternatively, try testing union() etc. without using labeling in the sub-selects:

    diff -r ee9102622c8538bd7e2594b05dd8bdc72317d3d0 lib/sqlalchemy/orm/query.py
    --- a/lib/sqlalchemy/orm/query.py   Sun Oct 03 13:11:41 2010 -0400
    +++ b/lib/sqlalchemy/orm/query.py   Sat Oct 09 11:28:53 2010 -0400
    @@ -1005,7 +1005,7 @@
    
    
             return self._from_selectable(
    -                    expression.union(*([self](self)+ list(q))))
    +                    expression.union(*([self.statement](self.statement)+ list(q))))
    
         def union_all(self, *q):
             """Produce a UNION ALL of this Query against one or more queries.
    

    test:

    from sqlalchemy import *
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import *
    
    Base = declarative_base()
    session = Session()
    
    class Foo(Base):
       __tablename__ = 'foo'
       id = Column(Integer, primary_key=True)
       spam = Column(String)
    
    class Bar(Base):
       __tablename__ = 'bar'
       id = Column(Integer, primary_key=True)
       spam = Column(String)
    
    qa = session.query(Foo.spam)
    qb = session.query(Bar.spam)
    
    print [x['name'](x['name') for x in qa.union(qb).column_descriptions]
    

    need to decide which approach is more appropriate. probably the former, since adapt_to_selectable currently only occurs in "from_self()" / "from_some_union(self)" types of situations.

  2. Log in to comment