From f90dfd45cd89c2919b747dad88d66b6bc29fd017 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 13 Jun 2018 15:59:35 -0400 Subject: Support JOIN in UPDATE..FROM The :class:`.Update` construct now accommodates a :class:`.Join` object as supported by MySQL for UPDATE..FROM. As the construct already accepted an alias object for a similar purpose, the feature of UPDATE against a non-table was already implied so this has been added. Change-Id: I7b2bca627849384d5377abb0c94626463e4fad04 Fixes: #3645 (cherry picked from commit 58540ae93db30fb12f331587c32bb2d76db79ab3) --- lib/sqlalchemy/sql/compiler.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index a442c65fd..f6cdebb16 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2178,6 +2178,16 @@ class SQLCompiler(Compiled): "selectable": update_stmt}) extra_froms = update_stmt._extra_froms + is_multitable = bool(extra_froms) + + if is_multitable: + # main table might be a JOIN + main_froms = set(selectable._from_objects(update_stmt.table)) + render_extra_froms = [ + f for f in extra_froms if f not in main_froms + ] + else: + render_extra_froms = [] text = "UPDATE " @@ -2186,8 +2196,7 @@ class SQLCompiler(Compiled): update_stmt._prefixes, **kw) table_text = self.update_tables_clause(update_stmt, update_stmt.table, - extra_froms, **kw) - + render_extra_froms, **kw) crud_params = crud._setup_crud_params( self, update_stmt, crud.ISUPDATE, **kw) @@ -2200,7 +2209,7 @@ class SQLCompiler(Compiled): text += table_text text += ' SET ' - include_table = extra_froms and \ + include_table = is_multitable and \ self.render_table_with_column_in_update_from text += ', '.join( c[0]._compiler_dispatch(self, @@ -2217,7 +2226,7 @@ class SQLCompiler(Compiled): extra_from_text = self.update_from_clause( update_stmt, update_stmt.table, - extra_froms, + render_extra_froms, dialect_hints, **kw) if extra_from_text: text += " " + extra_from_text -- cgit v1.2.1