From 6370d6b15f52abdbbadca3707e3722b984daff53 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 9 Aug 2022 13:31:14 -0400 Subject: 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 | 23 +++++++++++++++++++++++ doc/build/orm/internals.rst | 10 +++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 doc/build/changelog/unreleased_20/8372.rst (limited to 'doc/build') diff --git a/doc/build/changelog/unreleased_20/8372.rst b/doc/build/changelog/unreleased_20/8372.rst new file mode 100644 index 000000000..97927198b --- /dev/null +++ b/doc/build/changelog/unreleased_20/8372.rst @@ -0,0 +1,23 @@ +.. change:: + :tags: bug, orm + :tickets: 8372 + + 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. diff --git a/doc/build/orm/internals.rst b/doc/build/orm/internals.rst index 73a6428e7..19c88d810 100644 --- a/doc/build/orm/internals.rst +++ b/doc/build/orm/internals.rst @@ -45,9 +45,8 @@ sections, are listed here. :members: __get__, __set__, __delete__ :undoc-members: -.. autodata:: MANYTOONE - -.. autodata:: MANYTOMANY +.. autoclass:: LoaderCallableStatus + :members: .. autoclass:: Mapped @@ -67,8 +66,6 @@ sections, are listed here. .. autofunction:: merge_frozen_result -.. autodata:: ONETOMANY - .. autoclass:: PropComparator :members: :inherited-members: @@ -77,6 +74,9 @@ sections, are listed here. :members: :inherited-members: +.. autoclass:: RelationshipDirection + :members: + .. autodata:: RelationshipProperty .. autoclass:: Synonym -- cgit v1.2.1