MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / one_or_none

Method one_or_none

lib/sqlalchemy/orm/query.py:2768–2792  ·  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

2766 return self.limit(1)._iter().first() # type: ignore
2767
2768 def one_or_none(self) -> Optional[_T]:
2769 """Return at most one result or raise an exception.
2770
2771 Returns ``None`` if the query selects
2772 no rows. Raises ``sqlalchemy.orm.exc.MultipleResultsFound``
2773 if multiple object identities are returned, or if multiple
2774 rows are returned for a query that returns only scalar values
2775 as opposed to full identity-mapped entities.
2776
2777 Calling :meth:`_query.Query.one_or_none`
2778 results in an execution of the
2779 underlying query.
2780
2781 .. seealso::
2782
2783 :meth:`_query.Query.first`
2784
2785 :meth:`_query.Query.one`
2786
2787 :meth:`_engine.Result.one_or_none` - v2 comparable method.
2788
2789 :meth:`_engine.Result.scalar_one_or_none` - v2 comparable method.
2790
2791 """
2792 return self._iter().one_or_none() # type: ignore
2793
2794 def one(self) -> _T:
2795 """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