# 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

    top_level_stmt = compiler.statement

    self.extra_criteria_entities = {}

    extra_criteria_attributes = {}

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

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

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

    DeleteDMLState.__init__(self, statement, 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 = {}

    extra_criteria_attributes = {}

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

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

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

    DeleteDMLState.__init__(self, statement, compiler, **kw)

    return self