From: Mike Bayer Date: Mon, 16 Oct 2006 23:34:23 +0000 (+0000) Subject: - added "column_prefix=None" argument to mapper; prepends the X-Git-Tag: rel_0_3_0~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=506b594f521252d1cabbfecfb3704c38e5a27642;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - added "column_prefix=None" argument to mapper; prepends the given string (typically '_') to column-based attributes automatically set up from the mapper's Table --- diff --git a/CHANGES b/CHANGES index cbc4bc0ce7..7413d081e5 100644 --- a/CHANGES +++ b/CHANGES @@ -158,6 +158,9 @@ - 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 diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 97c4bd58c5..dd1ca0f757 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -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))) diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 4a816c16c2..2c7eab385f 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -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()