from .base import _declarative_constructor
from .base import _DeferredMapperConfig
from .base import _del_attribute
+from .base import _get_immediate_cls_attr
from .clsregistry import _class_resolver
from ... import exc
from ... import inspection
'polymorphic_identity':'manager',
'concrete':True}
+
+ .. note::
+
+ By default the name of the discriminator column used in the
+ :func:`.polymorphic_union` will be ``type``. This name might clash with
+ a column in one of your mapped tables. For that reason, the name of
+ the discriminator can be configured by setting the ``_concrete_discriminator_name`` attribute.
+
+ Example::
+
+ class Employee(ConcreteBase, Base):
+ _concrete_discriminator_name = '_concrete_discriminator'
+
+
.. seealso::
:class:`.AbstractConcreteBase`
"""
@classmethod
- def _create_polymorphic_union(cls, mappers):
+ def _create_polymorphic_union(cls, mappers, discriminator_name):
return polymorphic_union(
OrderedDict(
(mp.polymorphic_identity, mp.local_table) for mp in mappers
),
- "type",
+ discriminator_name,
"pjoin",
)
if m.with_polymorphic:
return
+ discriminator_name = _get_immediate_cls_attr(cls, "_concrete_discriminator_name") or "type"
+
mappers = list(m.self_and_descendants)
- pjoin = cls._create_polymorphic_union(mappers)
+ pjoin = cls._create_polymorphic_union(mappers, discriminator_name)
m._set_with_polymorphic(("*", pjoin))
- m._set_polymorphic_on(pjoin.c.type)
+ m._set_polymorphic_on(pjoin.c[discriminator_name])
class AbstractConcreteBase(ConcreteBase):
mn = _mapper_or_none(klass)
if mn is not None:
mappers.append(mn)
- pjoin = cls._create_polymorphic_union(mappers)
+
+ discriminator_name = _get_immediate_cls_attr(cls, "_concrete_discriminator_name") or "type"
+ pjoin = cls._create_polymorphic_union(mappers, discriminator_name)
# For columns that were declared on the class, these
# are normally ignored with the "__no_table__" mapping,
def mapper_args():
args = m_args()
- args["polymorphic_on"] = pjoin.c.type
+ args["polymorphic_on"] = pjoin.c[discriminator_name]
return args
to_map.mapper_args_fn = mapper_args