I have a column_property on a polymorphic base class. When I 
joinedload/subqueryload a derived class the colum_property makes the query 
fail.

class A(Base):
    __tablename__ = "a"
    id      = Column(Integer, primary_key=True)
    type    = Column(String(40), nullable=False)
    __mapper_args__ = {'polymorphic_on': type}

A.anything = orm.column_property(A.id + 1000)

class B(A):
    __tablename__ = "b"
    account_id      = Column(Integer, ForeignKey('a.id'), primary_key=True)
    x_id            = Column(Integer, ForeignKey('x.id'), nullable=False)
    __mapper_args__ = {'polymorphic_identity': 'named'}

class X(Base):
    __tablename__ = "x"
    id = Column(Integer, primary_key=True)
    b = orm.relationship("B"


Calling:
    print Session.query(X).options(joinedload("b"))

produces:

SELECT x.id AS x_id,
       anon_1.a_id AS anon_1_a_id,
       anon_1.a_type AS anon_1_a_type,
       a.id + %(id_1)s AS anon_2,
                     anon_1.b_account_id AS anon_1_b_account_id,
                     anon_1.b_x_id AS anon_1_b_x_idFROM a,
     xLEFT OUTER JOIN
  (SELECT a.id AS a_id,
          a.TYPE AS a_type,
            b.account_id AS b_account_id,
            b.x_id AS b_x_id
   FROM a
   JOIN b ON a.id = b.account_id) AS anon_1 ON x.id = anon_1.b_x_id


It seems that the "a.id + %(id_1)" should changed to "anon_1.a_id" and "a" 
removed from "FROM" or better "a.id + %(id_1)s" should be moved into the 
sub select named anon_1. This is probably what you want if the 
column_property was actually a subselect itself (which is want I'm actually 
trying to do).

Am I correct in thinking that this corner case simply isn't supported yet? 
Or is it a bug? Or am I doing something wrong?

Actually the above query doesn't fail outright. Although it does create a 
unsuspecting join. But if the column_property instead was something like

class subA(Base):
    __tablename__ = "subA"
    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'), nullable=False)
    value = Column(Integer, nullable=False)

A.anything = orm.column_property(
    select([func.sum(subA.value)], subA.a_id==A.id))

Then the sql would be:

SELECT x.id AS x_id,
       anon_1.a_id AS anon_1_a_id,
       anon_1.a_type AS anon_1_a_type,

  (SELECT sum("subA".value) AS sum_1
   FROM "subA"
   WHERE "subA".a_id = a.id) AS anon_2,
       anon_1.b_account_id AS anon_1_b_account_id,
       anon_1.b_x_id AS anon_1_b_x_idFROM xLEFT OUTER JOIN
  (SELECT a.id AS a_id,
          a.TYPE AS a_type,
            b.account_id AS b_account_id,
            b.x_id AS b_x_id
   FROM a
   JOIN b ON a.id = b.account_id) AS anon_1 ON x.id = anon_1.b_x_id

    
Which naturally doesn't work at all, since "a.id" inside the first 
subselect doesn't refer to anything.


PS: I have no idea how this email is going to get formatted, please let me 
know if it is impossible to read.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/1CSullHjqPMJ.
To post to this group, send email to sqlalchemy@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.

Reply via email to