From: Mike Bayer Date: Sat, 24 Sep 2011 00:51:58 +0000 (-0400) Subject: - Modified Column.copy() to use _constructor(), X-Git-Tag: rel_0_7_3~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4c04590a6438f73278675e96dd84b57121eeb5f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Modified Column.copy() to use _constructor(), which defaults to self.__class__, in order to create the new object. This allows easier support of subclassing Column. [ticket:2284] --- diff --git a/CHANGES b/CHANGES index 598b8be672..1f5cd15bda 100644 --- a/CHANGES +++ b/CHANGES @@ -133,6 +133,11 @@ CHANGES [ticket:2270]. Also in 0.6.9. - schema + - Modified Column.copy() to use _constructor(), + which defaults to self.__class__, in order to + create the new object. This allows easier support + of subclassing Column. [ticket:2284] + - Added a slightly nicer __repr__() to SchemaItem classes. Note the repr here can't fully support the "repr is the constructor" idea since schema diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 8fd4e75781..99c8acbfe2 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -990,7 +990,7 @@ class Column(SchemaItem, expression.ColumnClause): [c.copy(**kw) for c in self.constraints] + \ [c.copy(**kw) for c in self.foreign_keys if not c.constraint] - c = Column( + c = self._constructor( name=self.name, type_=self.type, key = self.key, diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 2107a3a77d..1f3a8e5c69 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -69,6 +69,21 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): assert a1 is not a2 eq_(a2._colspec, 'bat.blah') + def test_col_subclass_copy(self): + class MyColumn(schema.Column): + def __init__(self, *args, **kw): + self.widget = kw.pop('widget', None) + super(MyColumn, self).__init__(*args, **kw) + + def copy(self, *arg, **kw): + c = super(MyColumn, self).copy(*arg, **kw) + c.widget = self.widget + return c + c1 = MyColumn('foo', Integer, widget='x') + c2 = c1.copy() + assert isinstance(c2, MyColumn) + eq_(c2.widget, 'x') + def test_uninitialized_column_copy_events(self): msgs = [] def write(c, t):