summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/mapper.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-10-10 14:03:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-10-11 14:39:54 -0400
commitc35921b79e1683d3738a69f8ff6a892c3c2846e2 (patch)
tree78493c245874882ebcedbdb7b6d623c5b4f1b4d9 /lib/sqlalchemy/orm/mapper.py
parent9d132a7cf96678a732b06266fa0a35279268604b (diff)
downloadsqlalchemy-c35921b79e1683d3738a69f8ff6a892c3c2846e2.tar.gz
warn for no polymorphic identity w/ poly hierarchy
A warning is emitted when attempting to configure a mapped class within an inheritance hierarchy where the mapper is not given any polymorphic identity, however there is a polymorphic discriminator column assigned. Such classes should be abstract if they never intend to load directly. Fixes: #7545 Change-Id: I94f04e59736c73e3f39d883a75d763e3f06ecc3d
Diffstat (limited to 'lib/sqlalchemy/orm/mapper.py')
-rw-r--r--lib/sqlalchemy/orm/mapper.py23
1 files changed, 20 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 36b97cf17..5d784498a 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -1224,10 +1224,27 @@ class Mapper(
else:
self.persist_selectable = self.local_table
- if self.polymorphic_identity is not None and not self.concrete:
- self._identity_class = self.inherits._identity_class
- else:
+ if self.polymorphic_identity is None:
+ self._identity_class = self.class_
+
+ if self.inherits.base_mapper.polymorphic_on is not None:
+ util.warn(
+ "Mapper %s does not indicate a polymorphic_identity, "
+ "yet is part of an inheritance hierarchy that has a "
+ "polymorphic_on column of '%s'. Objects of this type "
+ "cannot be loaded polymorphically which can lead to "
+ "degraded or incorrect loading behavior in some "
+ "scenarios. Please establish a polmorphic_identity "
+ "for this class, or leave it un-mapped. "
+ "To omit mapping an intermediary class when using "
+ "declarative, set the '__abstract__ = True' "
+ "attribute on that class."
+ % (self, self.inherits.base_mapper.polymorphic_on)
+ )
+ elif self.concrete:
self._identity_class = self.class_
+ else:
+ self._identity_class = self.inherits._identity_class
if self.version_id_col is None:
self.version_id_col = self.inherits.version_id_col