# Buggy code
def _replace(self, col):
    PrimaryKeyConstraint._autoincrement_column._reset(self)
    self.columns.replace(col)


# Traceback (IndexError)
Traceback (most recent call last):
File "/home/kshutt/.config/JetBrains/PyCharm2020.3/scratches/scratch_4.py", line 41, in <module>
table.__table__.create(engine)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 928, in create
bind._run_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2095, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1656, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 145, in traverse_single
return meth(obj, **kw)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 832, in visit_table
include_foreign_key_constraints,  # noqa
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
return meth(self, multiparams, params)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1066, in _execute_ddl
else None,
File "<string>", line 1, in <lambda>
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 481, in compile
return self._compiler(dialect, bind=bind, **kw)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 29, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 322, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 352, in process
return obj._compiler_dispatch(self, **kwargs)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 96, in _compiler_dispatch
return meth(self, **kw)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 2961, in visit_create_table
_include_foreign_key_constraints=create.include_foreign_key_constraints,  # noqa
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3012, in create_table_constraints
for constraint in constraints
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3009, in <genexpr>
p
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3019, in <genexpr>
or not getattr(constraint, "use_alter", False)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 352, in process
return obj._compiler_dispatch(self, **kwargs)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 96, in _compiler_dispatch
return meth(self, **kw)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 1928, in visit_primary_key_constraint
constraint
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3238, in visit_primary_key_constraint
formatted_name = self.preparer.format_constraint(constraint)
File "<string>", line 1, in <lambda>
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3779, in format_constraint
return self.quote(name)
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3701, in quote
if self._requires_quotes(ident):
File "/home/kshutt/mandate/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 3614, in _requires_quotes
or value[0] in self.illegal_initial_characters
IndexError: string index out of range

# Fixed code
def _replace(self, col):
    PrimaryKeyConstraint._autoincrement_column._reset(self)
    self.columns.replace(col)

    self.dispatch._sa_event_column_added_to_pk_constraint(self, col)