# Buggy code
def create_for_statement(cls, statement, compiler, **kw):
    self = cls.__new__(cls)

    ext_info = statement.table._annotations["parententity"]

    self.mapper = mapper = ext_info.mapper

    self.extra_criteria_entities = {}

    self._resolved_values = cls._get_resolved_values(mapper, statement)

    extra_criteria_attributes = {}

    for opt in statement._with_options:
        if opt._is_criteria_option:
            opt.get_global_criteria(extra_criteria_attributes)

    if not statement._preserve_parameter_order and statement._values:
        self._resolved_values = dict(self._resolved_values)

    new_stmt = sql.Update.__new__(sql.Update)
    new_stmt.__dict__.update(statement.__dict__)
    new_stmt.table = mapper.local_table

    # note if the statement has _multi_values, these
    # are passed through to the new statement, which will then raise
    # InvalidRequestError because UPDATE doesn't support multi_values
    # right now.
    if statement._ordered_values:
        new_stmt._ordered_values = self._resolved_values
    elif statement._values:
        new_stmt._values = self._resolved_values

    new_crit = cls._adjust_for_extra_criteria(extra_criteria_attributes, mapper)
    if new_crit:
        new_stmt = new_stmt.where(*new_crit)

    # if we are against a lambda statement we might not be the
    # topmost object that received per-execute annotations
    top_level_stmt = compiler.statement
    if (
        top_level_stmt._annotations.get("synchronize_session", None) == "fetch"
        and compiler.dialect.full_returning
    ):
        new_stmt = new_stmt.returning(*mapper.primary_key)

    UpdateDMLState.__init__(self, new_stmt, compiler, **kw)

    return self


# Traceback (AttributeError)
Traceback (most recent call last):
File "./reproduction.py", line 315, in <module>
Score.__table__.create()
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 925, in create
bind._run_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2095, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1656, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 145, in traverse_single
return meth(obj, **kw)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 854, in visit_table
_is_metadata_operation=_is_metadata_operation,
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
fn(*args, **kw)
File "./reproduction.py", line 152, in create_after_all_dependencies
CreateTrigger(self)(*args, **kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 256, in __call__
return bind.execute(self.against(target))
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
return meth(self, multiparams, params)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1066, in _execute_ddl
else None,
File "<string>", line 1, in <lambda>
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 481, in compile
return self._compiler(dialect, bind=bind, **kw)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 29, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 325, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 355, in process
return obj._compiler_dispatch(self, **kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py", line 441, in <lambda>
lambda *arg, **kw: existing(*arg, **kw),
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py", line 486, in __call__
return fn(element, compiler, **kw)
File "./reproduction.py", line 199, in visit_create_trigger_mysql
compiled_query = sql_compiler.process(element.trigger.query, literal_binds=True)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 355, in process
return obj._compiler_dispatch(self, **kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 96, in _compiler_dispatch
return meth(self, **kw)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 2515, in visit_insert
insert_stmt._post_values_clause, **kw
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 355, in process
return obj._compiler_dispatch(self, **kwargs)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 96, in _compiler_dispatch
return meth(self, **kw)
File "….virtualenvs/yeepa/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1400, in visit_on_duplicate_key_update
cols = self.statement.table.c
AttributeError: 'MySQLCompiler_mysqldb' object has no attribute 'statement'

# Fixed code
def create_for_statement(cls, statement, compiler, **kw):
    self = cls.__new__(cls)

    ext_info = statement.table._annotations["parententity"]

    self.mapper = mapper = ext_info.mapper

    self.extra_criteria_entities = {}

    self._resolved_values = cls._get_resolved_values(mapper, statement)

    extra_criteria_attributes = {}

    for opt in statement._with_options:
        if opt._is_criteria_option:
            opt.get_global_criteria(extra_criteria_attributes)

    if not statement._preserve_parameter_order and statement._values:
        self._resolved_values = dict(self._resolved_values)

    new_stmt = sql.Update.__new__(sql.Update)
    new_stmt.__dict__.update(statement.__dict__)
    new_stmt.table = mapper.local_table

    # note if the statement has _multi_values, these
    # are passed through to the new statement, which will then raise
    # InvalidRequestError because UPDATE doesn't support multi_values
    # right now.
    if statement._ordered_values:
        new_stmt._ordered_values = self._resolved_values
    elif statement._values:
        new_stmt._values = self._resolved_values

    new_crit = cls._adjust_for_extra_criteria(extra_criteria_attributes, mapper)
    if new_crit:
        new_stmt = new_stmt.where(*new_crit)

    # if we are against a lambda statement we might not be the
    # topmost object that received per-execute annotations

    if (
        compiler._annotations.get("synchronize_session", None) == "fetch"
        and compiler.dialect.full_returning
    ):
        new_stmt = new_stmt.returning(*mapper.primary_key)

    UpdateDMLState.__init__(self, new_stmt, compiler, **kw)

    return self