]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- query.scalar() now raises an exception if more than one
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Mar 2010 23:38:51 +0000 (18:38 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Mar 2010 23:38:51 +0000 (18:38 -0500)
row is returned.  All other behavior remains the same.
[ticket:1735]

CHANGES
lib/sqlalchemy/orm/query.py
test/orm/test_query.py

diff --git a/CHANGES b/CHANGES
index 101c0223bde5deb7b520b82b186d0c1cb2b25f31..4335efd8a3225327c6ac566630ec27b5979ae6dd 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -109,6 +109,10 @@ CHANGES
     query.from_statement() raises an exception now instead of 
     silently dropping those criterion. [ticket:1736]
     
+  - query.scalar() now raises an exception if more than one 
+    row is returned.  All other behavior remains the same.
+    [ticket:1735]
+    
 - sql
   - The most common result processors conversion function were
     moved to the new "processors" module.  Dialect authors are
index 88945b9d078354b21e42bb4cf3cb2141d21da610..d7b60dfa3cc7f6be5c445e0544aaf03bc3c01c5e 100644 (file)
@@ -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):
index 054d64d20411d487c67eae6844b36b0ae4a5fb6c..a2212851d4646ad25e33eb93fb8681566261fcbe 100644 (file)
@@ -2924,7 +2924,10 @@ class ImmediateTest(_fixtures.FixtureTest):
         eq_(sess.query(User.id).filter_by(id=0).scalar(), None)
         eq_(sess.query(User).filter_by(id=7).scalar(),
             sess.query(User).filter_by(id=7).one())
-
+        
+        assert_raises(sa.orm.exc.MultipleResultsFound, sess.query(User).scalar)
+        assert_raises(sa.orm.exc.MultipleResultsFound, sess.query(User.id, User.name).scalar)
+        
     @testing.resolve_artifact_names
     def test_value(self):
         sess = create_session()