diff options
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/ext/baked.py | 20 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 12 |
2 files changed, 30 insertions, 2 deletions
diff --git a/lib/sqlalchemy/ext/baked.py b/lib/sqlalchemy/ext/baked.py index f01e0b348..a933e7eb7 100644 --- a/lib/sqlalchemy/ext/baked.py +++ b/lib/sqlalchemy/ext/baked.py @@ -283,6 +283,26 @@ class Result(object): raise orm_exc.MultipleResultsFound( "Multiple rows were found for one()") + def one_or_none(self): + """Return one or zero results, or raise an exception for multiple + rows. + + Equivalent to :meth:`.Query.one_or_none`. + + .. versionadded:: 1.0.9 + + """ + ret = list(self) + + l = len(ret) + if l == 1: + return ret[0] + elif l == 0: + return None + else: + raise orm_exc.MultipleResultsFound( + "Multiple rows were found for one()") + def all(self): """Return all rows. diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 8c4925905..f0b8969a2 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -2491,8 +2491,10 @@ class Query(object): Calling ``one_or_none()`` results in an execution of the underlying query. - .. versionchanged:: 1.0.9 - Added ``one_or_none()`` + .. versionadded:: 1.0.9 + + Added :meth:`.Query.one_or_none` + """ ret = list(self) @@ -2526,6 +2528,12 @@ class Query(object): any kind of limit, so that the "unique"-ing of entities does not conceal multiple object identities. + .. seealso:: + + :meth:`.Query.first` + + :meth:`.Query.one_or_none` + """ ret = list(self) |