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

Method first

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

2736 return self
2737
2738 def first(self) -> Optional[_T]:
2739 """Return the first result of this ``Query`` or
2740 None if the result doesn't contain any row.
2741
2742 first() applies a limit of one within the generated SQL, so that
2743 only one primary entity row is generated on the server side
2744 (note this may consist of multiple result rows if join-loaded
2745 collections are present).
2746
2747 Calling :meth:`_query.Query.first`
2748 results in an execution of the underlying
2749 query.
2750
2751 .. seealso::
2752
2753 :meth:`_query.Query.one`
2754
2755 :meth:`_query.Query.one_or_none`
2756
2757 :meth:`_engine.Result.first` - v2 comparable method.
2758
2759 :meth:`_engine.Result.scalars` - v2 comparable method.
2760
2761 """
2762 # replicates limit(1) behavior
2763 if self._statement is not None:
2764 return self._iter().first() # type: ignore
2765 else:
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.

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