# Buggy code
def _set_parent(self, table):
    if not self.name:
        raise exc.ArgumentError(
            "Column must be constructed with a non-blank name or "
            "assign a non-blank .name before adding to a Table."
        )
    if self.key is None:
        self.key = self.name

    existing = getattr(self, "table", None)
    if existing is not None and existing is not table:
        raise exc.ArgumentError(
            "Column object '%s' already assigned to Table '%s'"
            % (self.key, existing.description)
        )

    if self.key in table._columns:
        col = table._columns.get(self.key)
        if col is not self:
            for fk in col.foreign_keys:
                table.foreign_keys.remove(fk)
                if fk.constraint in table.constraints:
                    # this might have been removed
                    # already, if it's a composite constraint
                    # and more than one col being replaced
                    table.constraints.remove(fk.constraint)

    table._columns.replace(self)

    if self.primary_key:
        table.primary_key._replace(self)
    elif self.key in table.primary_key:
        raise exc.ArgumentError(
            "Trying to redefine primary-key column '%s' as a "
            "non-primary-key column on table '%s'" % (self.key, table.fullname)
        )

    self.table = table

    if self.index:
        if isinstance(self.index, util.string_types):
            raise exc.ArgumentError(
                "The 'index' keyword argument on Column is boolean only. "
                "To create indexes with a specific name, create an "
                "explicit Index object external to the Table."
            )
        table.append_constraint(
            Index(None, self.key, unique=bool(self.unique), _column_flag=True)
        )

    elif self.unique:
        if isinstance(self.unique, util.string_types):
            raise exc.ArgumentError(
                "The 'unique' keyword argument on Column is boolean "
                "only. To create unique constraints or indexes with a "
                "specific name, append an explicit UniqueConstraint to "
                "the Table's list of elements, or create an explicit "
                "Index object external to the Table."
            )
        table.append_constraint(UniqueConstraint(self.key, _column_flag=True))

    self._setup_on_memoized_fks(lambda fk: fk._set_remote_table(table))


# 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 _set_parent(self, table):
    if not self.name:
        raise exc.ArgumentError(
            "Column must be constructed with a non-blank name or "
            "assign a non-blank .name before adding to a Table."
        )
    if self.key is None:
        self.key = self.name

    existing = getattr(self, "table", None)
    if existing is not None and existing is not table:
        raise exc.ArgumentError(
            "Column object '%s' already assigned to Table '%s'"
            % (self.key, existing.description)
        )

    if self.key in table._columns:
        col = table._columns.get(self.key)
        if col is not self:
            for fk in col.foreign_keys:
                table.foreign_keys.remove(fk)
                if fk.constraint in table.constraints:
                    # this might have been removed
                    # already, if it's a composite constraint
                    # and more than one col being replaced
                    table.constraints.remove(fk.constraint)

    table._columns.replace(self)

    self.table = table

    if self.primary_key:
        table.primary_key._replace(self)
    elif self.key in table.primary_key:
        raise exc.ArgumentError(
            "Trying to redefine primary-key column '%s' as a "
            "non-primary-key column on table '%s'" % (self.key, table.fullname)
        )

    if self.index:
        if isinstance(self.index, util.string_types):
            raise exc.ArgumentError(
                "The 'index' keyword argument on Column is boolean only. "
                "To create indexes with a specific name, create an "
                "explicit Index object external to the Table."
            )
        table.append_constraint(
            Index(None, self.key, unique=bool(self.unique), _column_flag=True)
        )

    elif self.unique:
        if isinstance(self.unique, util.string_types):
            raise exc.ArgumentError(
                "The 'unique' keyword argument on Column is boolean "
                "only. To create unique constraints or indexes with a "
                "specific name, append an explicit UniqueConstraint to "
                "the Table's list of elements, or create an explicit "
                "Index object external to the Table."
            )
        table.append_constraint(UniqueConstraint(self.key, _column_flag=True))

    self._setup_on_memoized_fks(lambda fk: fk._set_remote_table(table))