]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
implement use_descriptor_defaults for dataclass defaults
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Dec 2024 15:59:25 +0000 (10:59 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 19 Mar 2025 19:14:37 +0000 (15:14 -0400)
commitc86ebb0a994682595562bd93d8ec7850ac228f17
treee8bec4c09280c375da9414aef2b501343c1e3fb3
parenta385bd9b50676d6f330471c182793467396bcdf9
implement use_descriptor_defaults for dataclass defaults

A significant behavioral change has been made to the behavior of the
:paramref:`_orm.mapped_column.default` and
:paramref:`_orm.relationship.default` parameters, when used with
SQLAlchemy's :ref:`orm_declarative_native_dataclasses` feature introduced
in 2.0, where the given value (assumed to be an immutable scalar value) is
no longer passed to the ``@dataclass`` API as a real default, instead a
token that leaves the value un-set in the object's ``__dict__`` is used, in
conjunction with a descriptor-level default.  This prevents an un-set
default value from overriding a default that was actually set elsewhere,
such as in relationship / foreign key assignment patterns as well as in
:meth:`_orm.Session.merge` scenarios.   See the full writeup in the
:ref:`whatsnew_21_toplevel` document which includes guidance on how to
re-enable the 2.0 version of the behavior if needed.

This adds a new implicit default field to ScalarAttributeImpl
so that we can have defaults that are not in the dictionary but
are instead passed through to the class-level descriptor, effectively
allowing custom defaults that are not used in INSERT or merge

Fixes: #12168
Change-Id: Ia327d18d6ec47c430e926ab7658e7b9f0666206e
17 files changed:
doc/build/changelog/migration_21.rst
doc/build/changelog/unreleased_21/12168.rst [new file with mode: 0644]
doc/build/faq/ormconfiguration.rst
lib/sqlalchemy/orm/_orm_constructors.py
lib/sqlalchemy/orm/attributes.py
lib/sqlalchemy/orm/base.py
lib/sqlalchemy/orm/decl_api.py
lib/sqlalchemy/orm/decl_base.py
lib/sqlalchemy/orm/descriptor_props.py
lib/sqlalchemy/orm/interfaces.py
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/orm/relationships.py
lib/sqlalchemy/orm/strategies.py
lib/sqlalchemy/orm/writeonly.py
lib/sqlalchemy/sql/schema.py
test/orm/declarative/test_dc_transforms.py
test/sql/test_metadata.py