MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / one_or_none

Method one_or_none

lib/sqlalchemy/orm/query.py:2825–2849  ·  view source on GitHub ↗

Return at most one result or raise an exception. Returns ``None`` if the query selects no rows. Raises ``sqlalchemy.orm.exc.MultipleResultsFound`` if multiple object identities are returned, or if multiple rows are returned for a query that returns only scalar value

(self)

Source from the content-addressed store, hash-verified

2823 return self.limit(1)._iter().first() # type: ignore
2824
2825 def one_or_none(self) -> Optional[_T]:
2826 """Return at most one result or raise an exception.
2827
2828 Returns ``None`` if the query selects
2829 no rows. Raises ``sqlalchemy.orm.exc.MultipleResultsFound``
2830 if multiple object identities are returned, or if multiple
2831 rows are returned for a query that returns only scalar values
2832 as opposed to full identity-mapped entities.
2833
2834 Calling :meth:`_query.Query.one_or_none`
2835 results in an execution of the
2836 underlying query.
2837
2838 .. seealso::
2839
2840 :meth:`_query.Query.first`
2841
2842 :meth:`_query.Query.one`
2843
2844 :meth:`_engine.Result.one_or_none` - v2 comparable method.
2845
2846 :meth:`_engine.Result.scalar_one_or_none` - v2 comparable method.
2847
2848 """
2849 return self._iter().one_or_none() # type: ignore
2850
2851 def one(self) -> _T:
2852 """Return exactly one result or raise an exception.

Callers 8

result_oneFunction · 0.45
test_one_or_noneMethod · 0.45
test_one_or_none_noneMethod · 0.45
test_one_or_noneMethod · 0.45
test_selectMethod · 0.45

Calls 1

_iterMethod · 0.95

Tested by 7

test_one_or_noneMethod · 0.36
test_one_or_none_noneMethod · 0.36
test_one_or_noneMethod · 0.36
test_selectMethod · 0.36