]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Declarative will accept a table-bound column as a property
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Feb 2009 19:35:36 +0000 (19:35 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 22 Feb 2009 19:35:36 +0000 (19:35 +0000)
when used in conjunction with __table__, if the column is already
present in __table__.  The column will be remapped to the given
key the same way as when added to the mapper() properties dict.

CHANGES
lib/sqlalchemy/ext/declarative.py
test/ext/declarative.py

diff --git a/CHANGES b/CHANGES
index c0916e3bb87c8aa421bc6e278c3351441212747c..ddff28fb7c5860a28f9c743e12eb301d9c81f0fa 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -108,6 +108,11 @@ CHANGES
     - Declarative will properly interpret the "foreign_keys" argument
       on a backref() if it's a string.
       
+    - Declarative will accept a table-bound column as a property
+      when used in conjunction with __table__, if the column is already
+      present in __table__.  The column will be remapped to the given 
+      key the same way as when added to the mapper() properties dict.
+      
 0.5.2
 ======
 
index 79677c084458fd92b74ff22b7a8ddc33e59bae16..084ec2699abc3eec48af61d963ac3ffa829c646c 100644 (file)
@@ -480,7 +480,9 @@ def _as_declarative(cls, classname, dict_):
     else:
         table = cls.__table__
         if cols:
-            raise exceptions.ArgumentError("Can't add additional columns when specifying __table__")
+            for c in cols:
+                if not table.c.contains_column(c):
+                    raise exceptions.ArgumentError("Can't add additional column %r when specifying __table__" % key)
             
     mapper_args = getattr(cls, '__mapper_args__', {})
     if 'inherits' not in mapper_args:
index 81009384b92092f0febd11f5a50c80757c189f3a..738f37495582a6bd1b63ea9dc8586d656cafe456 100644 (file)
@@ -65,12 +65,20 @@ class DeclarativeTest(DeclarativeTestBase):
         self.assertRaisesMessage(sa.exc.InvalidRequestError, "does not have a __table__", go)
 
     def test_cant_add_columns(self):
-        t = Table('t', Base.metadata, Column('id', Integer, primary_key=True))
+        t = Table('t', Base.metadata, Column('id', Integer, primary_key=True), Column('data', String))
         def go():
             class User(Base):
                 __table__ = t
                 foo = Column(Integer, primary_key=True)
-        self.assertRaisesMessage(sa.exc.ArgumentError, "add additional columns", go)
+        # can't specify new columns not already in the table
+        self.assertRaisesMessage(sa.exc.ArgumentError, "Can't add additional column 'foo' when specifying __table__", go)
+
+        # regular re-mapping works tho
+        class Bar(Base):
+            __table__ = t
+            some_data = t.c.data
+            
+        assert class_mapper(Bar).get_property('some_data').columns[0] is t.c.data
     
     def test_undefer_column_name(self):
         # TODO: not sure if there was an explicit