Repository: kayak/pypika
Pull request #296 (2019-08-16 11:22:15)

Fix orderby replace table function


Patch:
diff --git a/pypika/queries.py b/pypika/queries.py
index 1ce1248..ac8da5c 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -520,7 +520,8 @@ class QueryBuilder(Selectable, Term):
         self._prewheres = self._prewheres.replace_table(current_table, new_table) if self._prewheres else None
         self._groupbys = [groupby.replace_table(current_table, new_table) for groupby in self._groupbys]
         self._havings = self._havings.replace_table(current_table, new_table) if self._havings else None
-        self._orderbys = [orderby.replace_table(current_table, new_table) for orderby in self._orderbys]
+        self._orderbys = [(orderby[0].replace_table(current_table, new_table), orderby[1])
+                          for orderby in self._orderbys]
         self._joins = [join.replace_table(current_table, new_table) for join in self._joins]
 
         if current_table in self._select_star_tables:


Test patch:
diff --git a/pypika/tests/test_query.py b/pypika/tests/test_query.py
index 2b82c9d..2998ebc 100644
--- a/pypika/tests/test_query.py
+++ b/pypika/tests/test_query.py
@@ -12,31 +12,41 @@ class QueryTablesTests(unittest.TestCase):
     table_a, table_b, table_c, table_d = Tables('a', 'b', 'c', 'd')
 
     def test_replace_table(self):
-        query = Query.from_(self.table_a).select(self.table_a.time)
+        query = Query \
+            .from_(self.table_a) \
+            .select(self.table_a.time)
         query = query.replace_table(self.table_a, self.table_b)
 
         self.assertEqual('SELECT "time" FROM "b"', str(query))
 
     def test_replace_only_specified_table(self):
-        query = Query.from_(self.table_a).select(self.table_a.time)
+        query = Query \
+            .from_(self.table_a) \
+            .select(self.table_a.time)
         query = query.replace_table(self.table_b, self.table_c)
 
         self.assertEqual('SELECT "time" FROM "a"', str(query))
 
     def test_replace_insert_table(self):
-        query = Query.into(self.table_a).insert(1)
+        query = Query \
+            .into(self.table_a) \
+            .insert(1)
         query = query.replace_table(self.table_a, self.table_b)
 
         self.assertEqual('INSERT INTO "b" VALUES (1)', str(query))
 
     def test_replace_update_table(self):
-        query = Query.update(self.table_a).set('foo', 'bar')
+        query = Query \
+            .update(self.table_a) \
+            .set('foo', 'bar')
         query = query.replace_table(self.table_a, self.table_b)
 
         self.assertEqual('UPDATE "b" SET "foo"=\'bar\'', str(query))
 
     def test_replace_delete_table(self):
-        query = Query.from_(self.table_a).delete()
+        query = Query \
+            .from_(self.table_a) \
+            .delete()
         query = query.replace_table(self.table_a, self.table_b)
 
         self.assertEqual('DELETE FROM "b"', str(query))
@@ -96,6 +106,17 @@ class QueryTablesTests(unittest.TestCase):
                          'ELSE \'It was someone else.\' END "who_was_it" '
                          'FROM "b"', str(query))
 
+    def test_replace_orderby_table(self):
+        query = Query \
+            .from_(self.table_a) \
+            .select(self.table_a.customer) \
+            .orderby(self.table_a.customer)
+        query = query.replace_table(self.table_a, self.table_b)
+
+        self.assertEqual('SELECT "customer" '
+                         'FROM "b" '
+                         'ORDER BY "customer"', str(query))
+
     def test_is_joined(self):
         q = Query.from_(self.table_a).join(self.table_b).on(self.table_a.foo == self.table_b.boo)