]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
allow backref named 'metadata' to not break _metadata_for_cls
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 May 2026 14:25:39 +0000 (10:25 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 May 2026 15:03:34 +0000 (11:03 -0400)
commit96f98a9119d79b4f6e6026249a4afcf3b4b98c8e
treec960ab6ab35b776e530c3a84b262df3b9d7069cb
parent808fd28297f36bf932443bae77ca5bb16bcbd4dd
allow backref named 'metadata' to not break _metadata_for_cls

Fixed regression caused by :ticket:`8068` where a ``backref``
named ``'metadata'`` on a mapped class would cause an
``AssertionError`` when the class also used string-based
relationship references (e.g. ``secondary="some_table"``).
The ``_metadata_for_cls()`` helper now checks
``isinstance(meta, MetaData)`` as a condition rather than
asserting, falling back to ``registry.metadata`` when the
class attribute has been overwritten by a backref.

A warning is now emitted when a Declarative attribute name is named
``metadata`` or ``registry``.  Previously, no warning was emitted for
``registry``, and using the name ``metadata`` would raise an
InvalidRequestError.   Since these names can be used for attributes
that are mapped as backrefs or using imperative mappings, usage
under Declarative has been relaxed for ``metadata`` but also warns
for both names as they may have unintended interactions with the
Declarative reserved names.

References: https://bugs.launchpad.net/nova/+bug/2154165
References: https://github.com/sqlalchemy/sqlalchemy/discussions/8619

Fixes: #13333
Change-Id: I0f3173bce9c8c8881bd6b8ea75102bb8ec92be8b
doc/build/changelog/unreleased_21/13333.rst [new file with mode: 0644]
lib/sqlalchemy/orm/decl_base.py
lib/sqlalchemy/orm/util.py
test/orm/declarative/test_basic.py
test/orm/declarative/test_clsregistry.py