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
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.
"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>
"""
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):
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()