]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed a regression introduced by the join rewriting feature of
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 1 Nov 2013 19:24:43 +0000 (15:24 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 1 Nov 2013 19:24:43 +0000 (15:24 -0400)
:ticket:`2369` and :ticket:`2587` where a nested join with one side
already an aliased select would fail to translate the ON clause on the
outside correctly; in the ORM this could be seen when using a
SELECT statement as a "secondary" table. [ticket:2858]

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_join_rewriting.py

index 27faaba1dfebd77f33d7017f3312040848585b1d..228a85c1fe0279db1c085434246c37767cde3f8d 100644 (file)
 .. changelog::
     :version: 0.9.0b2
 
+    .. change::
+        :tags: bug, orm, sql, sqlite
+        :tickets: 2858
+
+        Fixed a regression introduced by the join rewriting feature of
+        :ticket:`2369` and :ticket:`2587` where a nested join with one side
+        already an aliased select would fail to translate the ON clause on the
+        outside correctly; in the ORM this could be seen when using a
+        SELECT statement as a "secondary" table.
+
 .. changelog::
     :version: 0.9.0b1
     :released: October 26, 2013
index 2bf7d3f4afafd3433d05d7283de5729682726147..4f3dbba3688ff172302d333a1ef454396c37dbff 100644 (file)
@@ -1289,9 +1289,11 @@ class SQLCompiler(Compiled):
                 for c in selectable_.c:
                     c._key_label = c.key
                     c._label = c.name
+
                 translate_dict = dict(
-                        zip(right.element.c, selectable_.c)
-                    )
+                    zip(newelem.right.element.c, selectable_.c)
+                )
+
                 translate_dict[right.element.left] = selectable_
                 translate_dict[right.element.right] = selectable_
 
@@ -1310,6 +1312,7 @@ class SQLCompiler(Compiled):
                 column_translate[-1].update(translate_dict)
 
                 newelem.right = selectable_
+
                 newelem.onclause = visit(newelem.onclause, **kw)
             elif newelem.__visit_name__ is select_name:
                 column_translate.append({})
index 3c670199c219b3e7e08a9d9e5fdf8e82d1aae8fd..801d5ce9a42788125900b4eea923c7ff805052cb 100644 (file)
@@ -55,6 +55,15 @@ class _JoinRewriteTestBase(AssertsCompiledSQL):
             key = key % compiled.anon_map
             assert col in compiled.result_map[key][1]
 
+    _a_bkeyselect_bkey = ""
+
+    def test_a_bkeyselect_bkey(self):
+        assoc = a_to_b_key.select().alias()
+        j1 = assoc.join(b_key)
+        j2 = a.join(j1)
+
+        s = select([a, b_key], use_labels=True).select_from(j2)
+        self._test(s, self._a_bkeyselect_bkey)
 
     def test_a_bc(self):
         j1 = b.join(c)
@@ -202,6 +211,18 @@ class JoinRewriteTest(_JoinRewriteTestBase, fixtures.TestBase):
         "anon_1 ON a.id = anon_1.a_to_b_key_1_aid"
         )
 
+    _a_bkeyselect_bkey = (
+        "SELECT a.id AS a_id, anon_2.anon_1_aid AS anon_1_aid, "
+        "anon_2.anon_1_bid AS anon_1_bid, anon_2.b_key_id AS b_key_id "
+        "FROM a JOIN (SELECT anon_1.aid AS anon_1_aid, anon_1.bid AS anon_1_bid, "
+            "b_key.id AS b_key_id "
+            "FROM (SELECT a_to_b_key.aid AS aid, a_to_b_key.bid AS bid "
+                "FROM a_to_b_key) AS anon_1 "
+        "JOIN b_key ON b_key.id = anon_1.bid) AS anon_2 ON a.id = anon_2.anon_1_aid"
+    )
+
+
+
 class JoinPlainTest(_JoinRewriteTestBase, fixtures.TestBase):
     """test rendering of each join with normal nesting."""
     @util.classproperty
@@ -209,6 +230,12 @@ class JoinPlainTest(_JoinRewriteTestBase, fixtures.TestBase):
         dialect = default.DefaultDialect()
         return dialect
 
+    _a_bkeyselect_bkey = (
+        "SELECT a.id AS a_id, b_key.id AS b_key_id FROM a JOIN "
+        "((SELECT a_to_b_key.aid AS aid, a_to_b_key.bid AS bid "
+            "FROM a_to_b_key) AS anon_1 JOIN b_key ON b_key.id = anon_1.bid) "
+        "ON a.id = anon_1.aid"
+    )
     _a__b_dc = (
             "SELECT a.id AS a_id, b.id AS b_id, "
             "b.a_id AS b_a_id, c.id AS c_id, "
@@ -274,6 +301,12 @@ class JoinNoUseLabelsTest(_JoinRewriteTestBase, fixtures.TestBase):
             assert_
         )
 
+    _a_bkeyselect_bkey = (
+        "SELECT a.id, b_key.id FROM a JOIN ((SELECT a_to_b_key.aid AS aid, "
+            "a_to_b_key.bid AS bid FROM a_to_b_key) AS anon_1 "
+            "JOIN b_key ON b_key.id = anon_1.bid) ON a.id = anon_1.aid"
+    )
+
     _a__b_dc = (
             "SELECT a.id, b.id, "
             "b.a_id, c.id, "