]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
validate mapped collection key is loaded
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 Aug 2022 17:31:14 +0000 (13:31 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 Aug 2022 18:25:41 +0000 (14:25 -0400)
commit6370d6b15f52abdbbadca3707e3722b984daff53
tree815ff17fa3abf13deb71c1b25e4ec0d4e1935e8a
parentd1187812efe0d85c050c9d99f59523bb7ecaa6ee
validate mapped collection key is loaded

Changed the attribute access method used by
:func:`_orm.attribute_mapped_collection` and
:func:`_orm.column_mapped_collection`, used when populating the dictionary,
to assert that the data value on the object to be used as the dictionary
key is actually present, and is not instead using "None" due to the
attribute never being actually assigned. This is used to prevent a
mis-population of None for a key when assigning via a backref where the
"key" attribute on the object is not yet assigned.

As the failure mode here is a transitory condition that is not typically
persisted to the database, and is easy to produce via the constructor of
the class based on the order in which parameters are assigned, it is very
possible that many applications include this behavior already which is
silently passed over. To accommodate for applications where this error is
now raised, a new parameter
:paramref:`_orm.attribute_mapped_collection.ignore_unpopulated_attribute`
is also added to both :func:`_orm.attribute_mapped_collection` and
:func:`_orm.column_mapped_collection` that instead causes the erroneous
backref assignment to be skipped.

Fixes: #8372
Change-Id: I85bf4af405adfefe6386f0f2f8cef22537d95912
doc/build/changelog/unreleased_20/8372.rst [new file with mode: 0644]
doc/build/orm/internals.rst
lib/sqlalchemy/orm/__init__.py
lib/sqlalchemy/orm/base.py
lib/sqlalchemy/orm/interfaces.py
lib/sqlalchemy/orm/mapped_collection.py
test/orm/test_collection.py