]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added "column_prefix=None" argument to mapper; prepends the
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Oct 2006 23:34:23 +0000 (23:34 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Oct 2006 23:34:23 +0000 (23:34 +0000)
given string (typically '_') to column-based attributes automatically
set up from the mapper's Table

CHANGES
lib/sqlalchemy/orm/mapper.py
test/orm/mapper.py

diff --git a/CHANGES b/CHANGES
index cbc4bc0ce7fbf099deac64e9e25ec44c960348ec..7413d081e5ec38953deb89980f44bd8ecddcd8ed 100644 (file)
--- a/CHANGES
+++ b/CHANGES
     - added "batch=True" flag to mapper; if False, save_obj
     will fully save one object at a time including calls
     to before_XXXX and after_XXXX
+    - added "column_prefix=None" argument to mapper; prepends the
+    given string (typically '_') to column-based attributes automatically
+    set up from the mapper's Table
     - specifying joins in the from_obj argument of query.select() will
     replace the main table of the query, if the table is somewhere within
     the given from_obj.  this makes it possible to produce custom joins and
index 97c4bd58c588d84246f5487d8ee1cf1b557e5243..dd1ca0f757472982d0cb27fe31e82ed6ea355d98 100644 (file)
@@ -53,7 +53,8 @@ class Mapper(object):
                 concrete=False,
                 select_table=None,
                 allow_null_pks=False,
-                batch=True):
+                batch=True,
+                column_prefix=None):
         """construct a new mapper.  
         
         All arguments may be sent to the sqlalchemy.orm.mapper() function where they are
@@ -120,6 +121,9 @@ class Mapper(object):
         setting to False indicates that an instance will be fully saved before saving the next instance, which 
         includes inserting/updating all table rows corresponding to the entity as well as calling all MapperExtension 
         methods corresponding to the save operation.
+
+        column_prefix - a string which will be prepended to the "key" name of all Columns when creating column-based
+        properties from the given Table.  does not affect explicitly specified column-based properties
         """
         if not issubclass(class_, object):
             raise exceptions.ArgumentError("Class '%s' is not a new-style class" % class_.__name__)
@@ -153,6 +157,7 @@ class Mapper(object):
         self.allow_null_pks = allow_null_pks
         self.delete_orphans = []
         self.batch = batch
+        self.column_prefix = column_prefix
         # a Column which is used during a select operation to retrieve the 
         # "polymorphic identity" of the row, which indicates which Mapper should be used
         # to construct a new object instance from that row.
@@ -449,19 +454,20 @@ class Mapper(object):
             if not self.columns.has_key(column.key):
                 self.columns[column.key] = self.select_table.corresponding_column(column, keys_ok=True, raiseerr=True)
 
+            column_key = (self.column_prefix or '') + column.key
             prop = self.__props.get(column.key, None)
             if prop is None:
                 prop = ColumnProperty(column)
-                self.__props[column.key] = prop
+                self.__props[column_key] = prop
                 prop.set_parent(self)
-                self.__log("adding ColumnProperty %s" % (column.key))
+                self.__log("adding ColumnProperty %s" % (column_key))
             elif isinstance(prop, ColumnProperty):
                 if prop.parent is not self:
                     prop = ColumnProperty(deferred=prop.deferred, group=prop.group, *prop.columns)
                     prop.set_parent(self)
-                    self.__props[column.key] = prop
+                    self.__props[column_key] = prop
                 prop.columns.append(column)
-                self.__log("appending to existing ColumnProperty %s" % (column.key))
+                self.__log("appending to existing ColumnProperty %s" % (column_key))
             else:
                 if not self.allow_column_override:
                     raise exceptions.ArgumentError("WARNING: column '%s' not being added due to property '%s'.  Specify 'allow_column_override=True' to mapper() to ignore this condition." % (column.key, repr(prop)))
index 4a816c16c2e5999d4aba41b3bdaae9b275053992..2c7eab385f285fab56e8f5c70ebec2042d8fcf5f 100644 (file)
@@ -63,7 +63,15 @@ class MapperTest(MapperSuperTest):
             assert False
         except exceptions.ArgumentError:
             pass
-        
+
+    def testcolumnprefix(self):
+        mapper(User, users, column_prefix='_')
+        s = create_session()
+        u = s.get(User, 7)
+        assert u._user_name=='jack'
+       assert u._user_id ==7
+        assert not hasattr(u, 'user_name')
+          
     def testrefresh(self):
         mapper(User, users, properties={'addresses':relation(mapper(Address, addresses))})
         s = create_session()