diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-11 18:38:51 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-11 18:38:51 -0500 |
commit | 3264433782034cf862e534ea6c9e07fd5531050c (patch) | |
tree | ff222ca0084051d580e8522c6e0fbeff420a865b /lib/sqlalchemy/orm/query.py | |
parent | 409c8ed703c7391380164c5d265c8401e65060c4 (diff) | |
download | sqlalchemy-3264433782034cf862e534ea6c9e07fd5531050c.tar.gz |
- query.scalar() now raises an exception if more than one
row is returned. All other behavior remains the same.
[ticket:1735]
Diffstat (limited to 'lib/sqlalchemy/orm/query.py')
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 88945b9d0..d7b60dfa3 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1309,6 +1309,11 @@ class Query(object): def first(self): """Return the first result of this ``Query`` or None if the result doesn't contain any row. + + first() applies a limit of one within the generated SQL, so that + only one primary entity row is generated on the server side + (note this may consist of multiple result rows if eagerly loaded + collections are present). Calling ``first()`` results in an execution of the underlying query. @@ -1354,7 +1359,9 @@ class Query(object): "Multiple rows were found for one()") def scalar(self): - """Return the first element of the first result or None. + """Return the first element of the first result or None + if no rows present. If multiple rows are returned, + raises MultipleResultsFound. >>> session.query(Item).scalar() <Item> @@ -1371,11 +1378,11 @@ class Query(object): """ try: - ret = list(self)[0] + ret = self.one() if not isinstance(ret, tuple): return ret return ret[0] - except IndexError: + except orm_exc.NoResultFound: return None def __iter__(self): |