# Buggy code
def copy(self, target_table=None, **kw):
    if target_table is not None:
        sqltext = _copy_expression(self.sqltext, self.table, target_table)
    else:
        sqltext = self.sqltext
    c = CheckConstraint(
        sqltext,
        name=self.name,
        initially=self.initially,
        deferrable=self.deferrable,
        _create_rule=self._create_rule,
        table=target_table,
        _autoattach=False,
        _type_bound=self._type_bound,
    )
    return self._schema_item_copy(c)


# Traceback (KeyError)
=============================================== FAILURES ================================================
_________________________________ ToMetaDataExcludeConstraint.test_copy _________________________________
Traceback (most recent call last):
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/sql/test_metadata.py", line 5238, in test_copy
table_c = table.tometadata(meta2)
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 1063, in tometadata
table.append_constraint(
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 867, in append_constraint
constraint._set_parent_with_dispatch(self)
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/base.py", line 463, in _set_parent_with_dispatch
self._set_parent(parent)
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 3065, in _set_parent
ColumnCollectionMixin._set_parent(self, table)
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 3022, in _set_parent
for col in self._col_expressions(table):
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 3016, in _col_expressions
return [
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/sql/schema.py", line 3017, in <listcomp>
table.c[col] if isinstance(col, util.string_types) else col
File "/home/jeffh/workspace/vortex-workspace/vfinance/subrepos/SQLAlchemy-1.3.18/test/../lib/sqlalchemy/util/_collections.py", line 194, in __getitem__
return self._data[key]
KeyError: "daterange(valid_from_date, valid_thru_date, '[]')"
======================================== short test summary info ========================================
FAILED test/sql/test_metadata.py::ToMetaDataExcludeConstraint::test_copy - KeyError: "daterange(valid_...

# Fixed code
def copy(self, target_table=None, **kw):
    if target_table is not None:
        # note that target_table is None for the copy process of
        # a column-bound CheckConstraint, so this path is not reached
        # in that case.
        sqltext = _copy_expression(self.sqltext, self.table, target_table)
    else:
        sqltext = self.sqltext
    c = CheckConstraint(
        sqltext,
        name=self.name,
        initially=self.initially,
        deferrable=self.deferrable,
        _create_rule=self._create_rule,
        table=target_table,
        _autoattach=False,
        _type_bound=self._type_bound,
    )
    return self._schema_item_copy(c)