From: Mike Bayer Date: Wed, 11 Mar 2009 21:45:57 +0000 (+0000) Subject: - Fixed bug where column_prefix wasn't being checked before X-Git-Tag: rel_0_5_3~6 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=3f0252abc77399b20ab263c70d5ec328c405c525;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug where column_prefix wasn't being checked before not mapping an attribute that already had class-level name present. --- diff --git a/CHANGES b/CHANGES index 14a9c16ab6..35af1e41b1 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,10 @@ CHANGES union(query1, query2), select([foo]).select_from(query), etc. + - Fixed bug where column_prefix wasn't being checked before + not mapping an attribute that already had class-level + name present. + - a session.expire() on a particular collection attribute will clear any pending backref additions as well, so that the next access correctly returns only what was present diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 98a027448e..0805407268 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -478,7 +478,7 @@ class Mapper(object): # pull properties from the inherited mapper if any. if self.inherits: for key, prop in self.inherits._props.iteritems(): - if key not in self._props and not self._should_exclude(key, local=False): + if key not in self._props and not self._should_exclude(key, key, local=False): self._adapt_inherited_property(key, prop, False) # create properties for each column in the mapped table, @@ -487,11 +487,11 @@ class Mapper(object): if column in self._columntoproperty: continue - if self._should_exclude(column.key, local=self.local_table.c.contains_column(column)): - continue - column_key = (self.column_prefix or '') + column.key + if self._should_exclude(column.key, column_key, local=self.local_table.c.contains_column(column)): + continue + # adjust the "key" used for this column to that # of the inheriting mapper for mapper in self.iterate_to_root(): @@ -509,7 +509,7 @@ class Mapper(object): col = self.polymorphic_on else: dont_instrument = False - if self._should_exclude(col.key, local=False): + if self._should_exclude(col.key, col.key, local=False): raise sa_exc.InvalidRequestError("Cannot exclude or override the discriminator column %r" % col.key) self._configure_property(col.key, ColumnProperty(col, _no_instrument=dont_instrument), init=False, setparent=True) @@ -937,7 +937,7 @@ class Mapper(object): def _is_userland_descriptor(self, obj): return not isinstance(obj, (MapperProperty, attributes.InstrumentedAttribute)) and hasattr(obj, '__get__') - def _should_exclude(self, name, local): + def _should_exclude(self, name, assigned_name, local): """determine whether a particular property should be implicitly present on the class. This occurs when properties are propagated from an inherited class, or are @@ -948,12 +948,12 @@ class Mapper(object): # check for descriptors, either local or from # an inherited class if local: - if self.class_.__dict__.get(name, None)\ - and self._is_userland_descriptor(self.class_.__dict__[name]): + if self.class_.__dict__.get(assigned_name, None)\ + and self._is_userland_descriptor(self.class_.__dict__[assigned_name]): return True else: - if getattr(self.class_, name, None)\ - and self._is_userland_descriptor(getattr(self.class_, name)): + if getattr(self.class_, assigned_name, None)\ + and self._is_userland_descriptor(getattr(self.class_, assigned_name)): return True if (self.include_properties is not None and diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 59ae025cb3..d2687fa5a0 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -443,6 +443,10 @@ class MapperTest(_fixtures.FixtureTest): class Hoho(object): pass class Lala(object): pass + class HasDef(object): + def name(self): + pass + p_m = mapper(Person, t, polymorphic_on=t.c.type, include_properties=('id', 'type', 'name')) e_m = mapper(Employee, inherits=p_m, polymorphic_identity='employee', @@ -460,6 +464,8 @@ class MapperTest(_fixtures.FixtureTest): l_m = mapper(Lala, t, exclude_properties=('vendor_id', 'boss_id'), column_prefix="p_") + hd_m = mapper(HasDef, t, column_prefix="h_") + p_m.compile() #sa.orm.compile_mappers() @@ -472,7 +478,8 @@ class MapperTest(_fixtures.FixtureTest): have = set([p.key for p in class_mapper(cls).iterate_properties]) want = set(want) eq_(have, want) - + + assert_props(HasDef, ['h_boss_id', 'h_employee_number', 'h_id', 'name', 'h_name', 'h_vendor_id', 'h_type']) assert_props(Person, ['id', 'name', 'type']) assert_instrumented(Person, ['id', 'name', 'type']) assert_props(Employee, ['boss', 'boss_id', 'employee_number',