inappropriate setting of polymorphic aliasing for a filter-level alias

Issue #2234 resolved
Mike Bayer repo owner created an issue

which you can see in a self-referential joined inh join from class to itself.

from sqlalchemy import *
from sqlalchemy.orm import *

class A(object):
    pass

class B(A):
    pass

m = MetaData()
a_table = Table('a', m,
    Column('id', Integer, primary_key=True),
)

b_table = Table('b', m,
    Column('id', Integer, ForeignKey('a.id'), primary_key=True),
    Column('b_id', Integer, ForeignKey('b.id')),
)

mapper(A, a_table, )
mapper(B ,b_table, inherits=A,
              properties={
                'bs':relationship(B, primaryjoin=b_table.c.id==b_table.c.b_id)
            })

s = Session()

print s.query(B).join(B.bs, aliased=True).statement

print s.query(B.id).join(B.bs, aliased=True).statement

and here's a patch !

diff -r 5c7cc274250c54430d9dceb05ad2a4a33e2378d7 lib/sqlalchemy/orm/query.py
--- a/lib/sqlalchemy/orm/query.py   Sun Jul 24 20:41:42 2011 -0400
+++ b/lib/sqlalchemy/orm/query.py   Wed Jul 27 18:07:58 2011 -0400
@@ -1535,7 +1535,7 @@
         # which is intended to wrap a the right side in a subquery,
         # ensure that columns retrieved from this target in the result
         # set are also adapted.
-        if aliased_entity:
+        if aliased_entity and not create_aliases:
             self.__mapper_loads_polymorphically_with(
                         right_mapper,
                         ORMAdapter(

Comments (2)

  1. Log in to comment