a kindler, gentler join()

Issue #1714 resolved
Mike Bayer repo owner created an issue

which would simulate a NATURAL JOIN if possible before searching all the FKs.

--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -2820,7 +2820,16 @@
         global sql_util
         if not sql_util:
             from sqlalchemy.sql import util as sql_util
-        return sql_util.join_condition(primary, secondary)
+
+        # First try the natural join
+        left = primary
+        while isinstance(left, Join):
+            left = left.right
+
+        try:
+            return sql_util.join_condition(left, secondary)
+        except exc.ArgumentError:
+            return sql_util.join_condition(primary, secondary)

     def select(self, whereclause=None, fold_equivalents=False, **kwargs):
         """Create a :class:`Select` from this :class:`Join`.

What I want to do here is basically the same thing but eliminate the ArgumentError throw, to reduce performance overhead, and additionally don't do join_condition() twice if "left" is the same as "primary".

Comments (2)

  1. Log in to comment