MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / first

Method first

lib/sqlalchemy/orm/query.py:2795–2823  ·  view source on GitHub ↗

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 joi

(self)

Source from the content-addressed store, hash-verified

2793 return self
2794
2795 def first(self) -> Optional[_T]:
2796 """Return the first result of this ``Query`` or
2797 None if the result doesn't contain any row.
2798
2799 first() applies a limit of one within the generated SQL, so that
2800 only one primary entity row is generated on the server side
2801 (note this may consist of multiple result rows if join-loaded
2802 collections are present).
2803
2804 Calling :meth:`_query.Query.first`
2805 results in an execution of the underlying
2806 query.
2807
2808 .. seealso::
2809
2810 :meth:`_query.Query.one`
2811
2812 :meth:`_query.Query.one_or_none`
2813
2814 :meth:`_engine.Result.first` - v2 comparable method.
2815
2816 :meth:`_engine.Result.scalars` - v2 comparable method.
2817
2818 """
2819 # replicates limit(1) behavior
2820 if self._statement is not None:
2821 return self._iter().first() # type: ignore
2822 else:
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.

Callers 15

has_sequenceMethod · 0.45
_internal_has_tableMethod · 0.45
_compat_firstMethod · 0.45
has_sequenceMethod · 0.45
test_subqueryMethod · 0.45
test_via_attrMethod · 0.45
test_via_stringMethod · 0.45
test_via_intMethod · 0.45
test_via_col_objectMethod · 0.45

Calls 2

_iterMethod · 0.95
limitMethod · 0.95

Tested by 15

test_subqueryMethod · 0.36
test_via_attrMethod · 0.36
test_via_stringMethod · 0.36
test_via_intMethod · 0.36
test_via_col_objectMethod · 0.36
_assert_tableMethod · 0.36