]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- additional test + correction for [ticket:2699]
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Apr 2013 14:34:59 +0000 (10:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Apr 2013 14:34:59 +0000 (10:34 -0400)
lib/sqlalchemy/orm/strategies.py
test/orm/test_subquery_relations.py

index e08bb40cb729f79e92bae857b306265f2e5b0221..0eed50ea419e87dc656ad43757cfcc12ecbbf35b 100644 (file)
@@ -778,11 +778,12 @@ class SubqueryLoader(AbstractRelationshipLoader):
         # to look only for significant columns
         q = orig_query._clone().correlate(None)
 
-        # TODO: why does polymporphic etc. require hardcoding
-        # into _adapt_col_list ?  Does query.add_columns(...) work
-        # with polymorphic loading ?
-        if entity_mapper.isa(leftmost_mapper):
+        # set a real "from" if not present, as this is more
+        # accurate than just going off of the column expression
+        if not q._from_obj and entity_mapper.isa(leftmost_mapper):
             q._set_select_from(entity_mapper)
+
+        # select from the identity columns of the outer
         q._set_entities(q._adapt_col_list(leftmost_attr))
 
         if q._order_by is False:
index 80dd73e983d66f13aef94d924a4b3a4c88456b70..3ee94cae9bc1df827841fb81856f9a56f5679ec1 100644 (file)
@@ -1036,7 +1036,7 @@ class BaseRelationFromJoinedSubclassTest(_Polymorphic):
         sess.add_all([e1, e2])
         sess.flush()
 
-    def test_correct_subquery(self):
+    def test_correct_subquery_nofrom(self):
         sess = create_session()
         # use Person.paperwork here just to give the least
         # amount of context
@@ -1083,6 +1083,57 @@ class BaseRelationFromJoinedSubclassTest(_Polymorphic):
                 )
         )
 
+    def test_correct_subquery_existingfrom(self):
+        sess = create_session()
+        # use Person.paperwork here just to give the least
+        # amount of context
+        q = sess.query(Engineer).\
+                filter(Engineer.primary_language == 'java').\
+                join(Engineer.paperwork).\
+                filter(Paperwork.description == "tps report #2").\
+                options(subqueryload(Person.paperwork))
+        def go():
+            eq_(q.one().paperwork,
+                    [Paperwork(description="tps report #1"),
+                    Paperwork(description="tps report #2")],
+
+                )
+        self.assert_sql_execution(
+            testing.db,
+            go,
+            CompiledSQL(
+                "SELECT people.person_id AS people_person_id, "
+                "people.name AS people_name, people.type AS people_type, "
+                "engineers.engineer_id AS engineers_engineer_id, "
+                "engineers.primary_language AS engineers_primary_language "
+                "FROM people JOIN engineers "
+                    "ON people.person_id = engineers.engineer_id "
+                    "JOIN paperwork ON people.person_id = paperwork.person_id "
+                "WHERE engineers.primary_language = :primary_language_1 "
+                "AND paperwork.description = :description_1",
+                {"primary_language_1": "java",
+                    "description_1": "tps report #2"}
+            ),
+            CompiledSQL(
+                "SELECT paperwork.paperwork_id AS paperwork_paperwork_id, "
+                "paperwork.description AS paperwork_description, "
+                "paperwork.person_id AS paperwork_person_id, "
+                "anon_1.people_person_id AS anon_1_people_person_id "
+                "FROM (SELECT people.person_id AS people_person_id "
+                "FROM people JOIN engineers ON people.person_id = "
+                "engineers.engineer_id JOIN paperwork "
+                "ON people.person_id = paperwork.person_id "
+                "WHERE engineers.primary_language = :primary_language_1 AND "
+                "paperwork.description = :description_1) AS anon_1 "
+                "JOIN paperwork ON anon_1.people_person_id = "
+                "paperwork.person_id "
+                "ORDER BY anon_1.people_person_id, paperwork.paperwork_id",
+                {"primary_language_1": "java",
+                    "description_1": "tps report #2"}
+            )
+        )
+
+
 
 
 class SelfReferentialTest(fixtures.MappedTest):