From 4873e9a910aceeb7ff0a8dfbc2a5591600e104cc Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 28 Aug 2009 19:00:55 +0000 Subject: [PATCH] - Fixed column.copy() to copy defaults and onupdates. [ticket:1373] --- CHANGES | 3 +++ lib/sqlalchemy/orm/mapper.py | 2 +- lib/sqlalchemy/schema.py | 16 +++++++++++++++- lib/sqlalchemy/test/testing.py | 9 --------- test/engine/test_metadata.py | 12 ++++++++++-- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGES b/CHANGES index 3ff1ee032b..2c33b78618 100644 --- a/CHANGES +++ b/CHANGES @@ -65,6 +65,9 @@ CHANGES is old stuff. [ticket:1486] - sql + - Fixed column.copy() to copy defaults and onupdates. + [ticket:1373] + - Fixed a bug in extract() introduced in 0.5.4 whereby the string "field" argument was getting treated as a ClauseElement, causing various errors within more diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 078056a01c..46b4c498b8 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -641,7 +641,7 @@ class Mapper(object): # when using the declarative layer self._post_configure_properties() return - _already_compiling = True +# _already_compiling = True try: # double-check inside mutex diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index e641f119b3..7ba835cc44 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -723,7 +723,21 @@ class Column(SchemaItem, expression.ColumnClause): This is used in ``Table.tometadata``. """ - return Column(self.name, self.type, self.default, key = self.key, primary_key = self.primary_key, nullable = self.nullable, quote=self.quote, index=self.index, autoincrement=self.autoincrement, *[c.copy(**kw) for c in self.constraints]) + return Column( + self.name, + self.type, + self.default, + key = self.key, + primary_key = self.primary_key, + nullable = self.nullable, + quote=self.quote, + index=self.index, + autoincrement=self.autoincrement, + default=self.default, + server_default=self.server_default, + onupdate=self.onupdate, + server_onupdate=self.server_onupdate, + *[c.copy(**kw) for c in self.constraints]) def _make_proxy(self, selectable, name=None): """Create a *proxy* for this column. diff --git a/lib/sqlalchemy/test/testing.py b/lib/sqlalchemy/test/testing.py index 36c7d340a3..c7afcf0b0d 100644 --- a/lib/sqlalchemy/test/testing.py +++ b/lib/sqlalchemy/test/testing.py @@ -580,15 +580,6 @@ class ComparesTables(object): eq_(c.type.length, reflected_c.type.length) eq_(set([f.column.name for f in c.foreign_keys]), set([f.column.name for f in reflected_c.foreign_keys])) - if c.default: - assert isinstance(reflected_c.server_default, - schema.FetchedValue) - elif against(('mysql', '<', (5, 0))): - # ignore reflection of bogus db-generated DefaultClause() - pass - elif not c.primary_key or not against('postgres'): - print repr(c) - assert reflected_c.default is None, reflected_c.default assert len(table.primary_key) == len(reflected_table.primary_key) for c in table.primary_key: diff --git a/test/engine/test_metadata.py b/test/engine/test_metadata.py index ca4fbaa48a..8680f31ea9 100644 --- a/test/engine/test_metadata.py +++ b/test/engine/test_metadata.py @@ -46,6 +46,8 @@ class MetaDataTest(TestBase, ComparesTables): table = Table('mytable', meta, Column('myid', Integer, primary_key=True), Column('name', String(40), nullable=True), + Column('foo', String(40), nullable=False, server_default='x', server_onupdate='q'), + Column('bar', String(40), nullable=False, default='y', onupdate='z'), Column('description', String(30), CheckConstraint("description='hi'")), UniqueConstraint('name'), test_needs_fk=True, @@ -83,7 +85,7 @@ class MetaDataTest(TestBase, ComparesTables): meta.create_all(testing.db) try: - for test, has_constraints in ((test_to_metadata, True), (test_pickle, True), (test_pickle_via_reflect, False)): + for test, has_constraints, reflect in ((test_to_metadata, True, False), (test_pickle, True, False),(test_pickle_via_reflect, False, True)): table_c, table2_c = test() self.assert_tables_equal(table, table_c) self.assert_tables_equal(table2, table2_c) @@ -92,7 +94,13 @@ class MetaDataTest(TestBase, ComparesTables): assert table.primary_key is not table_c.primary_key assert list(table2_c.c.myid.foreign_keys)[0].column is table_c.c.myid assert list(table2_c.c.myid.foreign_keys)[0].column is not table.c.myid - + assert 'x' in str(table_c.c.foo.server_default.arg) + + if not reflect: + assert str(table_c.c.foo.server_onupdate.arg) == 'q' + assert str(table_c.c.bar.default.arg) == 'y' + assert getattr(table_c.c.bar.onupdate.arg, 'arg', table_c.c.bar.onupdate.arg) == 'z' + # constraints dont get reflected for any dialect right now if has_constraints: for c in table_c.c.description.constraints: -- 2.47.3