diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2022-12-14 22:35:51 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2022-12-14 22:35:51 +0000 |
| commit | 0f5b3040a419fad404d3e2ea8abfb781be228066 (patch) | |
| tree | dfeef45dd5221d99b2c75ae3811db3a8c5d312d6 /lib/sqlalchemy | |
| parent | f954672103892b165097d70124d7fa354ef8bd4f (diff) | |
| parent | 2bac535f506dd68595d64d6cdf21e2d4937c2c9f (diff) | |
| download | sqlalchemy-0f5b3040a419fad404d3e2ea8abfb781be228066.tar.gz | |
Merge "warn when backref will replace existing userland descriptor" into main
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 20 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/relationships.py | 4 |
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 7a7524621..93b6c4ecb 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1658,7 +1658,7 @@ class Mapper( ) continue - self._configure_property(key, possible_col_prop, False) + self._configure_property(key, possible_col_prop, init=False) # step 2: pull properties from the inherited mapper. reconcile # columns with those which are explicit above. for properties that @@ -1698,7 +1698,7 @@ class Mapper( # it now in the order in which it corresponds to the # Table / selectable key, prop = explicit_col_props_by_column[column] - self._configure_property(key, prop, False) + self._configure_property(key, prop, init=False) continue elif column in self._columntoproperty: @@ -1962,8 +1962,10 @@ class Mapper( self, key: str, prop_arg: Union[KeyedColumnElement[Any], MapperProperty[Any]], + *, init: bool = True, setparent: bool = True, + warn_for_existing: bool = False, ) -> MapperProperty[Any]: descriptor_props = util.preloaded.orm_descriptor_props self._log( @@ -2073,6 +2075,20 @@ class Mapper( oldprop = self._props[key] self._path_registry.pop(oldprop, None) + if ( + warn_for_existing + and self.class_.__dict__.get(key, None) is not None + and not isinstance( + self._props.get(key, None), + (descriptor_props.ConcreteInheritedProperty,), + ) + ): + util.warn( + "User-placed attribute %r on %s being replaced with " + 'new property "%s"; the old attribute will be discarded' + % (self.class_.__dict__[key], self, prop) + ) + self._props[key] = prop if not self.non_primary: diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 2a659bdef..9852fa9a9 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -2108,7 +2108,9 @@ class RelationshipProperty( back_populates=self.key, **kwargs, ) - mapper._configure_property(backref_key, relationship) + mapper._configure_property( + backref_key, relationship, warn_for_existing=True + ) if self.back_populates: self._add_reverse_property(self.back_populates) |
