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

Method first

lib/sqlalchemy/engine/result.py:989–1026  ·  view source on GitHub ↗

Fetch the first row or ``None`` if no row is present. Closes the result set and discards remaining rows. .. note:: This method returns one **row**, e.g. tuple, by default. To return exactly one single scalar value, that is, the first column of the first row,

(self)

Source from the content-addressed store, hash-verified

987 return self._allrows()
988
989 def first(self) -> Optional[Row[Unpack[_Ts]]]:
990 """Fetch the first row or ``None`` if no row is present.
991
992 Closes the result set and discards remaining rows.
993
994 .. note:: This method returns one **row**, e.g. tuple, by default.
995 To return exactly one single scalar value, that is, the first
996 column of the first row, use the
997 :meth:`_engine.Result.scalar` method,
998 or combine :meth:`_engine.Result.scalars` and
999 :meth:`_engine.Result.first`.
1000
1001 Additionally, in contrast to the behavior of the legacy ORM
1002 :meth:`_orm.Query.first` method, **no limit is applied** to the
1003 SQL query which was invoked to produce this
1004 :class:`_engine.Result`;
1005 for a DBAPI driver that buffers results in memory before yielding
1006 rows, all rows will be sent to the Python process and all but
1007 the first row will be discarded.
1008
1009 .. seealso::
1010
1011 :ref:`migration_20_unify_select`
1012
1013 :return: a :class:`_engine.Row` object, or None
1014 if no rows remain.
1015
1016 .. seealso::
1017
1018 :meth:`_engine.Result.scalar`
1019
1020 :meth:`_engine.Result.one`
1021
1022 """
1023
1024 return self._only_one_row(
1025 raise_for_second_row=False, raise_for_none=False, scalar=False
1026 )
1027
1028 def one_or_none(self) -> Optional[Row[Unpack[_Ts]]]:
1029 """Return at most one result or raise an exception.

Callers

nothing calls this directly

Calls 1

_only_one_rowMethod · 0.80

Tested by

no test coverage detected