diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2018-06-25 18:38:11 -0400 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci.zzzcomputing.com> | 2018-06-25 18:38:11 -0400 |
| commit | 677818707471db670d5c3a83b10ebbc65f871881 (patch) | |
| tree | a33703267eec24e2ce7d837f4885eb2258b0cdd0 | |
| parent | c99345ee9994c3ea2a5e6536cc3365f18d017cc1 (diff) | |
| parent | 58540ae93db30fb12f331587c32bb2d76db79ab3 (diff) | |
| download | sqlalchemy-677818707471db670d5c3a83b10ebbc65f871881.tar.gz | |
Merge "Support JOIN in UPDATE..FROM"
| -rw-r--r-- | doc/build/changelog/unreleased_12/3645.rst | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 17 | ||||
| -rw-r--r-- | test/sql/test_update.py | 46 |
3 files changed, 67 insertions, 4 deletions
diff --git a/doc/build/changelog/unreleased_12/3645.rst b/doc/build/changelog/unreleased_12/3645.rst new file mode 100644 index 000000000..e750744b1 --- /dev/null +++ b/doc/build/changelog/unreleased_12/3645.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, mysql + :tickets: 3645 + + 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. diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 0b98dc51c..75827b34e 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2183,6 +2183,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 " @@ -2191,8 +2201,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) @@ -2205,7 +2214,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, @@ -2222,7 +2231,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 diff --git a/test/sql/test_update.py b/test/sql/test_update.py index 9ebaddffd..cc5b4962b 100644 --- a/test/sql/test_update.py +++ b/test/sql/test_update.py @@ -481,6 +481,23 @@ class UpdateFromCompileTest(_UpdateFromTestBase, fixtures.TablesTest, dialect='mysql' ) + def test_update_from_join_mysql(self): + users, addresses = self.tables.users, self.tables.addresses + + j = users.join(addresses) + self.assert_compile( + update(j). + values(name='newname'). + where(addresses.c.email_address == 'e1'), + "" + 'UPDATE users ' + 'INNER JOIN addresses ON users.id = addresses.user_id ' + 'SET users.name=%s ' + 'WHERE ' + 'addresses.email_address = %s', + checkparams={'email_address_1': 'e1', 'name': 'newname'}, + dialect=mysql.dialect()) + def test_render_table(self): users, addresses = self.tables.users, self.tables.addresses @@ -670,6 +687,35 @@ class UpdateFromRoundTripTest(_UpdateFromTestBase, fixtures.TablesTest): self._assert_users(users, expected) @testing.only_on('mysql', 'Multi table update') + def test_exec_join_multitable(self): + users, addresses = self.tables.users, self.tables.addresses + + values = { + addresses.c.email_address: 'updated', + users.c.name: 'ed2' + } + + testing.db.execute( + update(users.join(addresses)). + values(values). + where(users.c.name == 'ed')) + + expected = [ + (1, 7, 'x', 'jack@bean.com'), + (2, 8, 'x', 'updated'), + (3, 8, 'x', 'updated'), + (4, 8, 'x', 'updated'), + (5, 9, 'x', 'fred@fred.com')] + self._assert_addresses(addresses, expected) + + expected = [ + (7, 'jack'), + (8, 'ed2'), + (9, 'fred'), + (10, 'chuck')] + self._assert_users(users, expected) + + @testing.only_on('mysql', 'Multi table update') def test_exec_multitable_same_name(self): users, addresses = self.tables.users, self.tables.addresses |
