From e07130c597422d5f9a5d734e1411d8fef0c2deff Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 13 Jan 2023 17:24:14 -0500 Subject: implement polymorphic_abstract=True feature Added a new parameter to :class:`_orm.Mapper` called :paramref:`_orm.Mapper.polymorphic_abstract`. The purpose of this directive is so that the ORM will not consider the class to be instantiated or loaded directly, only subclasses. The actual effect is that the :class:`_orm.Mapper` will prevent direct instantiation of instances of the class and will expect that the class does not have a distinct polymorphic identity configured. In practice, the class that is mapped with :paramref:`_orm.Mapper.polymorphic_abstract` can be used as the target of a :func:`_orm.relationship` as well as be used in queries; subclasses must of course include polymorphic identities in their mappings. The new parameter is automatically applied to classes that subclass the :class:`.AbstractConcreteBase` class, as this class is not intended to be instantiated. Additionally, updated some areas of the single table inheritance documentation to include mapped_column(nullable=False) for all subclass-only columns; the mappings as given didn't work as the columns were no longer nullable using Annotated Declarative Table style. Fixes: #9060 Change-Id: Ief0278e3945a33a6ff38ac14d39c38ce24910d7f --- lib/sqlalchemy/ext/declarative/extensions.py | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/sqlalchemy/ext/declarative') diff --git a/lib/sqlalchemy/ext/declarative/extensions.py b/lib/sqlalchemy/ext/declarative/extensions.py index f5bae0695..2cb55a5ae 100644 --- a/lib/sqlalchemy/ext/declarative/extensions.py +++ b/lib/sqlalchemy/ext/declarative/extensions.py @@ -301,6 +301,7 @@ class AbstractConcreteBase(ConcreteBase): def mapper_args(): args = m_args() args["polymorphic_on"] = pjoin.c[discriminator_name] + args["polymorphic_abstract"] = True if strict_attrs: args["include_properties"] = ( set(pjoin.primary_key) -- cgit v1.2.1