diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-03-30 09:00:49 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-03-30 09:00:49 -0400 |
| commit | b4c1f29413f678c43d03afdaedd18bd18b23d59a (patch) | |
| tree | 1fd4c4f55f7bc4a03f1d5f0f7cbfe9b10081a4a6 /lib/sqlalchemy | |
| parent | 41e4fdb08722ec9843663500c6121e18c1bb0016 (diff) | |
| download | sqlalchemy-b4c1f29413f678c43d03afdaedd18bd18b23d59a.tar.gz | |
skip anno-only mixin columns that are overridden on subclasses
Fixed issue where an annotation-only :class:`_orm.Mapped` directive could
not be used in a Declarative mixin class, without that attribute attempting
to take effect for single- or joined-inheritance subclasses of mapped
classes that had already mapped that attribute on a superclass, producing
conflicting column errors and/or warnings.
Fixes: #9564
Change-Id: I0f92be2ae98a8c45afce3e06d0a7cc61c19a96f4
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/decl_base.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py index d01aad439..87166142e 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -616,7 +616,7 @@ class _ClassScanMapperConfig(_MapperConfig): if not sa_dataclass_metadata_key: def attribute_is_overridden(key: str, obj: Any) -> bool: - return getattr(cls, key) is not obj + return getattr(cls, key, obj) is not obj else: @@ -973,7 +973,10 @@ class _ClassScanMapperConfig(_MapperConfig): # then test if this name is already handled and # otherwise proceed to generate. if not fixed_table: - assert name in collected_attributes + assert ( + name in collected_attributes + or attribute_is_overridden(name, None) + ) continue else: # here, the attribute is some other kind of @@ -1300,11 +1303,22 @@ class _ClassScanMapperConfig(_MapperConfig): # copy mixin columns to the mapped class for name, obj, annotation, is_dataclass in attributes_for_class(): + if ( not fixed_table and obj is None and _is_mapped_annotation(annotation, cls, originating_class) ): + # obj is None means this is the annotation only path + + if attribute_is_overridden(name, obj): + # perform same "overridden" check as we do for + # Column/MappedColumn, this is how a mixin col is not + # applied to an inherited subclass that does not have + # the mixin. the anno-only path added here for + # #9564 + continue + collected_annotation = self._collect_annotation( name, annotation, originating_class, True, obj ) |
