From 29838ef584d49e5ecca08f76e4966454dc7f060f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 25 Sep 2022 14:56:22 -0400 Subject: warn for local-only column in remote side A warning is emitted in ORM configurations when an explicit :func:`_orm.remote` annotation is applied to columns that are local to the immediate mapped class, when the referenced class does not include any of the same table columns. Ideally this would raise an error at some point as it's not correct from a mapping point of view. Fixes: #7094 Fixes: #8575 Change-Id: Ia31be24aebe143161e19dc311b52c08fd5014d33 --- lib/sqlalchemy/orm/relationships.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 45b9b9bea..48d60647c 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -2806,6 +2806,22 @@ class JoinCondition: "condition that are on the remote side of " "the relationship." % (self.prop,) ) + else: + + not_target = util.column_set( + self.parent_persist_selectable.c + ).difference(self.child_persist_selectable.c) + + for _, rmt in self.local_remote_pairs: + if rmt in not_target: + util.warn( + "Expression %s is marked as 'remote', but these " + "column(s) are local to the local side. The " + "remote() annotation is needed only for a " + "self-referential relationship where both sides " + "of the relationship refer to the same tables." + % (rmt,) + ) def _check_foreign_cols( self, join_condition: ColumnElement[bool], primary: bool -- cgit v1.2.1