From e26487e234e4192db7e75cedf029df6de83f3415 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Wed, 12 May 2021 21:09:41 +0200 Subject: Detect back_populates referring to non-relationship Change-Id: Ic26d5d6d230d8d3209e6e42ff7752b59b8cb7b28 --- lib/sqlalchemy/orm/relationships.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 2ed9d859a..7e97962f4 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -2051,6 +2051,13 @@ class RelationshipProperty(StrategizedProperty): def _add_reverse_property(self, key): other = self.mapper.get_property(key, _configure_mappers=False) + if not isinstance(other, RelationshipProperty): + raise sa_exc.InvalidRequestError( + "back_populates on relationship '%s' refers to attribute '%s' " + "that is not a relationship. The back_populates parameter " + "should refer to the name of a relationship on the target " + "class." % (self, other) + ) # viewonly and sync_backref cases # 1. self.viewonly==True and other.sync_backref==True -> error # 2. self.viewonly==True and other.viewonly==False and -- cgit v1.2.1