summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/interfaces.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-03-16 12:30:13 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2011-03-16 12:30:13 -0400
commit464835e409dbd607a8a1fbbc8399f6c0c14b3ea8 (patch)
treeb27f0bd1c8c54b0a20b9d7aa8abfda63e62dfc4f /lib/sqlalchemy/orm/interfaces.py
parent92c8979d4a4c87cba7a68fd8d1766c3e8c0049b7 (diff)
downloadsqlalchemy-464835e409dbd607a8a1fbbc8399f6c0c14b3ea8.tar.gz
- Improvements to the error messages emitted when
querying against column-only entities in conjunction with (typically incorrectly) using loader options, where the parent entity is not fully present. [ticket:2069]
Diffstat (limited to 'lib/sqlalchemy/orm/interfaces.py')
-rw-r--r--lib/sqlalchemy/orm/interfaces.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/sqlalchemy/orm/interfaces.py b/lib/sqlalchemy/orm/interfaces.py
index 907dd4bf5..8055aa504 100644
--- a/lib/sqlalchemy/orm/interfaces.py
+++ b/lib/sqlalchemy/orm/interfaces.py
@@ -422,7 +422,7 @@ class PropertyOption(MapperOption):
state['key'] = tuple(ret)
self.__dict__ = state
- def _find_entity( self, query, mapper, raiseerr):
+ def _find_entity_prop_comparator(self, query, token, mapper, raiseerr):
if mapperutil._is_aliased_class(mapper):
searchfor = mapper
isa = False
@@ -435,9 +435,27 @@ class PropertyOption(MapperOption):
return ent
else:
if raiseerr:
- raise sa_exc.ArgumentError("Can't find entity %s in "
- "Query. Current list: %r" % (searchfor,
- [str(m.path_entity) for m in query._entities]))
+ raise sa_exc.ArgumentError(
+ "Can't find property '%s' on any entity "
+ "specified in this Query." % (token,)
+ )
+ else:
+ return None
+
+ def _find_entity_basestring(self, query, token, raiseerr):
+ for ent in query._mapper_entities:
+ # return only the first _MapperEntity when searching
+ # based on string prop name. Ideally object
+ # attributes are used to specify more exactly.
+ return ent
+ else:
+ if raiseerr:
+ raise sa_exc.ArgumentError(
+ "Can't find property named '%s' on the first mapped "
+ "entity in this Query. "
+ "Consider using an attribute object instead of a "
+ "string name to target a specific entity." % (token, )
+ )
else:
return None
@@ -459,13 +477,13 @@ class PropertyOption(MapperOption):
sub_tokens = token.split(".", 1)
token = sub_tokens[0]
tokens.extendleft(sub_tokens[1:])
-
if not entity:
if current_path:
if current_path[1] == token:
current_path = current_path[2:]
continue
- entity = query._entity_zero()
+ entity = self._find_entity_basestring(query,
+ token, raiseerr)
path_element = entity.path_entity
mapper = entity.mapper
mappers.append(mapper)
@@ -473,7 +491,6 @@ class PropertyOption(MapperOption):
prop = mapper.get_property(token)
else:
prop = None
- key = token
elif isinstance(token, PropComparator):
prop = token.property
if not entity:
@@ -482,14 +499,13 @@ class PropertyOption(MapperOption):
prop.key]:
current_path = current_path[2:]
continue
- entity = self._find_entity(query,
- token.parententity, raiseerr)
+ entity = self._find_entity_prop_comparator(query,
+ prop.key, token.parententity, raiseerr)
if not entity:
return [], []
path_element = entity.path_entity
mapper = entity.mapper
mappers.append(prop.parent)
- key = prop.key
else:
raise sa_exc.ArgumentError('mapper option expects '
'string key or list of attributes')