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)
| 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. |