]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
support configurable None behavior for composites
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 May 2025 12:36:05 +0000 (08:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 26 Aug 2025 18:33:05 +0000 (14:33 -0400)
commit071abbb8636d81ff0c9a4ea8b8a972e63cf5ef54
tree566e8367f61fb1963c10820aa746f45c6bed016d
parentb939befa507b9f400dcee3d7285547a178d6b088
support configurable None behavior for composites

Added new parameter :paramref:`_orm.composite.return_none_on` to
:func:`_orm.composite`, which allows control over if and when this
composite attribute should resolve to ``None`` when queried or retrieved
from the object directly.  By default, a composite object is always present
on the attribute, including for a pending object which is a behavioral
change since 2.0.   When :paramref:`_orm.composite.return_none_on` is
specified, a callable is passed that returns True or False to indicate if
the given arguments indicate the composite should be returned as None. This
parameter may also be set automatically when ORM Annotated Declarative is
used; if the annotation is given as ``Mapped[SomeClass|None]``, a
:paramref:`_orm.composite.return_none_on` rule is applied that will return
``None`` if all contained columns are themselves ``None``.

Fixes: #12570
Change-Id: Iab01ac46065689da1332c9f80dedbc7eb0f5380b
doc/build/changelog/migration_21.rst
doc/build/changelog/unreleased_21/12570.rst [new file with mode: 0644]
doc/build/orm/composites.rst
lib/sqlalchemy/orm/_orm_constructors.py
lib/sqlalchemy/orm/descriptor_props.py
test/orm/declarative/test_tm_future_annotations_sync.py
test/orm/declarative/test_typed_mapping.py
test/orm/test_composites.py