diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-02 03:23:33 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-01-02 03:23:33 +0000 |
commit | 9be9e473c535d5ea9fa2c55766cdfb47012af8df (patch) | |
tree | f4337a1324b95b8a7a87a13722189a1160629a54 | |
parent | 67e63bc7c472d959f3fac954afd1858e18dd4453 (diff) | |
download | sqlalchemy-9be9e473c535d5ea9fa2c55766cdfb47012af8df.tar.gz |
- The copy() method on Column now supports uninitialized,
unnamed Column objects. This allows easy creation of
declarative helpers which place common columns on multiple
subclasses.
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/schema.py | 20 | ||||
-rw-r--r-- | test/engine/test_metadata.py | 21 |
3 files changed, 41 insertions, 6 deletions
@@ -6,6 +6,12 @@ CHANGES 0.5.8 ===== +- sql + - The copy() method on Column now supports uninitialized, + unnamed Column objects. This allows easy creation of + declarative helpers which place common columns on multiple + subclasses. + - postgresql - The extract() function, which was slightly improved in 0.5.7, needed a lot more work to generate the correct diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 7ba835cc4..ce095799b 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -723,10 +723,21 @@ class Column(SchemaItem, expression.ColumnClause): This is used in ``Table.tometadata``. """ + + if self.args: + # if not Table initialized + args = [] + for a in self.args: + if isinstance(a, Constraint): + a = a.copy() + args.append(a) + else: + # if Table initialized + args = [c.copy(**kw) for c in self.constraints] + return Column( - self.name, - self.type, - self.default, + name=self.name, + type_=self.type, key = self.key, primary_key = self.primary_key, nullable = self.nullable, @@ -737,7 +748,8 @@ class Column(SchemaItem, expression.ColumnClause): server_default=self.server_default, onupdate=self.onupdate, server_onupdate=self.server_onupdate, - *[c.copy(**kw) for c in self.constraints]) + *args + ) def _make_proxy(self, selectable, name=None): """Create a *proxy* for this column. diff --git a/test/engine/test_metadata.py b/test/engine/test_metadata.py index 8680f31ea..6f1a45e5d 100644 --- a/test/engine/test_metadata.py +++ b/test/engine/test_metadata.py @@ -1,7 +1,7 @@ from sqlalchemy.test.testing import assert_raises, assert_raises_message import pickle from sqlalchemy import MetaData -from sqlalchemy import Integer, String, UniqueConstraint, CheckConstraint, ForeignKey +from sqlalchemy import Integer, String, UniqueConstraint, CheckConstraint, ForeignKey, Sequence from sqlalchemy.test.schema import Table from sqlalchemy.test.schema import Column import sqlalchemy as tsa @@ -20,7 +20,24 @@ class MetaDataTest(TestBase, ComparesTables): finally: metadata.drop_all() - + def test_uninitialized_column_copy(self): + for col in [ + Column('foo', String(), nullable=False), + Column(Integer(), primary_key=True), + Column('bar', Integer(), Sequence('foo_seq'), primary_key=True, key='bar'), + Column(Integer(), ForeignKey('bat.blah')), + Column('bar', Integer(), ForeignKey('bat.blah'), primary_key=True, key='bar'), + ]: + c2 = col.copy() + for attr in ('name', 'type', 'nullable', 'primary_key', 'key'): + eq_(getattr(col, attr), getattr(c2, attr)) + eq_(len(col.args), len(c2.args)) + for a1, a2 in zip(col.args, c2.args): + if isinstance(a1, ForeignKey): + assert a2._colspec == 'bat.blah' + elif isinstance(a1, Sequence): + assert a2.name == 'foo_seq' + def test_dupe_tables(self): metadata = MetaData() t1 = Table('table1', metadata, Column('col1', Integer, primary_key=True), |