]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
allow callable for relationship.back_populates
authorPriyanshu Parikh <parikhpriyanshu7@gmail.com>
Sun, 15 Oct 2023 14:34:25 +0000 (10:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 Jan 2024 16:14:20 +0000 (11:14 -0500)
commit95464adc3c81827bd1c072674dc8c4e17463d8cb
tree98daec9bd0266bcd5b4b338134cfc92e35b11075
parent071d3e2d2b11a96fc5a143530357244177259189
allow callable for relationship.back_populates

The :paramref:`_orm.relationship.back_populates` argument to
:func:`_orm.relationship` may now be passed as a Python callable, which
resolves to either the direct linked ORM attribute, or a string value as
before.  ORM attributes are also accepted directly by
:paramref:`_orm.relationship.back_populates`.   This change allows type
checkers and IDEs to confirm the argument for
:paramref:`_orm.relationship.back_populates` is valid. Thanks to Priyanshu
Parikh for the help on suggesting and helping to implement this feature.

An attempt was made to do this for ForeignKey as well, however this
is not feasible since there is no "deferred configuration" step
for Table objects; Table objects set each other up on ForeignKey
as they are created, such as setting the type of a column
in a referencing Column when the referenced table is created.  We
have no way to know which Table a foreign key intends to represent
when it's a callable whereas when it's a string, we do know, and
we actually make a lot of use of that string to match it to the
target table as that target is created (see _fk_memos).  However
the commit keeps a little bit of the cleanup to ForeignKey intact.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #10050
Closes: #10260
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10260
Pull-request-sha: 6f21002e1d5bbe291111655f33b19e4eb4b3cb84

Change-Id: I8e0a40c9898ec91d44f2df06dcc22f33b06745c3
doc/build/changelog/migration_21.rst
doc/build/changelog/unreleased_21/10050.rst [new file with mode: 0644]
lib/sqlalchemy/orm/_orm_constructors.py
lib/sqlalchemy/orm/relationships.py
lib/sqlalchemy/sql/_typing.py
lib/sqlalchemy/sql/schema.py
test/orm/test_relationships.py