From 498db831718cb5df213b1afdd2027878e0e72fd4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 18 Feb 2021 10:43:16 -0500 Subject: Extract table names when comparing to nrte error Fixed issue where the process of joining two tables could fail if one of the tables had an unrelated, unresolvable foreign key constraint which would raise :class:`_exc.NoReferenceError` within the join process, which nonetheless could be bypassed to allow the join to complete. The logic which tested the exception for signficance within the process would make assumptions about the construct which would fail. Fixes: #5952 Change-Id: I492dacd082ddcf8abb1310ed447a6ed734595bb7 --- lib/sqlalchemy/sql/selectable.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index ee2c4dafc..7c53f437c 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1229,9 +1229,12 @@ class Join(roles.DMLTableRole, FromClause): return bool(constraints) @classmethod + @util.preload_module("sqlalchemy.sql.util") def _joincond_scan_left_right( cls, a, a_subset, b, consider_as_foreign_keys ): + sql_util = util.preloaded.sql_util + a = coercions.expect(roles.FromClauseRole, a) b = coercions.expect(roles.FromClauseRole, b) @@ -1251,7 +1254,8 @@ class Join(roles.DMLTableRole, FromClause): try: col = fk.get_referent(left) except exc.NoReferenceError as nrte: - if nrte.table_name == left.name: + table_names = {t.name for t in sql_util.find_tables(left)} + if nrte.table_name in table_names: raise else: continue @@ -1270,7 +1274,8 @@ class Join(roles.DMLTableRole, FromClause): try: col = fk.get_referent(b) except exc.NoReferenceError as nrte: - if nrte.table_name == b.name: + table_names = {t.name for t in sql_util.find_tables(b)} + if nrte.table_name in table_names: raise else: continue -- cgit v1.2.1