diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-04-19 16:52:54 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-04-19 16:52:54 -0400 |
commit | 430ce5eab26d46301ae741f9068f13ba09907d8e (patch) | |
tree | ea6dbb81e735a62dcd2675cbf7b3bea073438320 /lib | |
parent | 2f617f56f2acdce00b88f746c403cf5ed66d4d27 (diff) | |
download | sqlalchemy-430ce5eab26d46301ae741f9068f13ba09907d8e.tar.gz |
Raise informative error when non-object m2o comparison used
An informative error message is raised when an ORM many-to-one comparison
is attempted against an object that is not an actual mapped instance.
Comparisons such as those to scalar subqueries aren't supported;
generalized comparison with subqueries is better achieved using
:meth:`~.RelationshipProperty.Comparator.has`.
Fixes: #5269
Change-Id: I2e23178eb59728c39241a46bfa7411239a87492e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sqlalchemy/orm/relationships.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 7d33c4649..6ac56a324 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -1621,8 +1621,19 @@ class RelationshipProperty(StrategizedProperty): alias_secondary=True, ): if state is not None: - state = attributes.instance_state(state) + try: + state = inspect(state) + except sa_exc.NoInspectionAvailable: + state = None + if state is None or not getattr(state, "is_instance", False): + raise sa_exc.ArgumentError( + "Mapped instance expected for relationship " + "comparison to object. Classes, queries and other " + "SQL elements are not accepted in this context; for " + "comparison with a subquery, " + "use %s.has(**criteria)." % self + ) reverse_direction = not value_is_parent if state is None: |