[Sqlalchemy-tickets] Issue #3645: UPDATE on a Join creates duplicate tables (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: immerrr <iss...@bi...> - 2016-02-04 23:03:52
|
New issue 3645: UPDATE on a Join creates duplicate tables https://bitbucket.org/zzzeek/sqlalchemy/issues/3645/update-on-a-join-creates-duplicate-tables immerrr: It is probably unexpected that it actually works, but MySQL can do that (and StackOverflow suggests that MS SQL can, too). Here's a script to reproduce: ``` import sqlalchemy as sa metadata = sa.MetaData() t1 = sa.Table( 't1', metadata, sa.Column('key1', sa.Integer), sa.Column('ref1', sa.Integer), sa.Column('val1', sa.Integer), sa.ForeignKeyConstraint(['ref1'], ['t2.key2']), ) t2 = sa.Table( 't2', metadata, sa.Column('key2', sa.Integer), sa.Column('val2', sa.Integer), ) engine = sa.create_engine('mysql+mysqlconnector://') def to_str(stmt): return str(stmt.compile(bind=engine, compile_kwargs={'literal_binds': True})) ``` In the end I see ``` >>> to_str( ... sa.update(t1.join(t2), ... values={t1.c.val1: 'foo'}, ... whereclause=(t2.c.val2 == 'foobar'))) ... 'UPDATE t1 INNER JOIN t2 ON t2.key2 = t1.ref1, t2 SET t1.val1=%(val1)s WHERE t2.val2 = %(val2_1)s' ``` Note, that there's a second `t2` table in the *table_references* part of the query. This patch seems to have fixed my case, but I can't seem to run tests out of the box, so I don't know if it broke anything else: ``` index 7b506f9..fe9f20b 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -766,7 +766,7 @@ class Update(ValuesBase): # TODO: this could be made memoized # if the memoization is reset on each generative call. froms = [] - seen = set([self.table]) + seen = set(_from_objects(self.table)) if self._whereclause is not None: for item in _from_objects(self._whereclause): ``` |