bound parameters in UPDATE..FROM are subject to mis-ordering

Issue #2768 resolved
Mike Bayer repo owner created an issue

this is a funny one:

from sqlalchemy.sql import table, column, select
from sqlalchemy.dialects import mysql

t1 = table('t1', column('x'))
t2 = table('t2', column('y'), column('z'))

subq = select([t2](t2)).where(t2.c.y == 7).alias()

stmt = t1.update().values(x=5).where(t1.c.x == subq.c.z)

compiled = stmt.compile(dialect=mysql.dialect())

# default impl, UPDATE..FROM .  y follows x
print stmt

# mysql impl, UPDATE A, B, x follows y
print compiled

# but still getting y follows x
print compiled.positiontup

that is:

UPDATE t1 SET x=:x FROM (SELECT t2.y AS y, t2.z AS z 
FROM t2 
WHERE t2.y = :y_1) AS anon_1 WHERE t1.x = anon_1.z

UPDATE t1, (SELECT t2.y AS y, t2.z AS z 
FROM t2 
WHERE t2.y = %s) AS anon_1 SET t1.x=%s WHERE t1.x = anon_1.z

[u'y_1']('x',)

Comments (3)

  1. Mike Bayer reporter

    OK very easy:

    diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
    index 0afcdfa..7770c7f 100644
    --- a/lib/sqlalchemy/sql/compiler.py
    +++ b/lib/sqlalchemy/sql/compiler.py
    @@ -1606,8 +1606,6 @@ class SQLCompiler(engine.Compiled):
    
             extra_froms = update_stmt._extra_froms
    
    -        colparams = self._get_colparams(update_stmt, extra_froms)
    -
             text = "UPDATE "
    
             if update_stmt._prefixes:
    @@ -1617,6 +1615,8 @@ class SQLCompiler(engine.Compiled):
             table_text = self.update_tables_clause(update_stmt, update_stmt.table,
                                                    extra_froms, **kw)
    
    +        colparams = self._get_colparams(update_stmt, extra_froms)
    +
             if update_stmt._hints:
                 dialect_hints = dict([
                     (table, hint_text)
    

    more of a test:

    d1 = DefaultDialect()
    d1.positional = True
    c1 = stmt.compile(dialect=d1)
    print c1
    print c1.positiontup
    
    compiled = stmt.compile(dialect=mysql.dialect())
    print compiled
    
    print compiled.positiontup
    
  2. Log in to comment