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

Method scalar

lib/sqlalchemy/orm/query.py:2817–2847  ·  view source on GitHub ↗

Return the first element of the first result or None if no rows present. If multiple rows are returned, raises :class:`_exc.MultipleResultsFound`. >>> session.query(Item).scalar() >>> session.query(Item.id).scalar() 1 >>> ses

(self)

Source from the content-addressed store, hash-verified

2815 return self._iter().one() # type: ignore
2816
2817 def scalar(self) -> Any:
2818 """Return the first element of the first result or None
2819 if no rows present. If multiple rows are returned,
2820 raises :class:`_exc.MultipleResultsFound`.
2821
2822 >>> session.query(Item).scalar()
2823 <Item>
2824 >>> session.query(Item.id).scalar()
2825 1
2826 >>> session.query(Item.id).filter(Item.id < 0).scalar()
2827 None
2828 >>> session.query(Item.id, Item.name).scalar()
2829 1
2830 >>> session.query(func.count(Parent.id)).scalar()
2831 20
2832
2833 This results in an execution of the underlying query.
2834
2835 .. seealso::
2836
2837 :meth:`_engine.Result.scalar` - v2 comparable method.
2838
2839 """
2840 # TODO: not sure why we can't use result.scalar() here
2841 try:
2842 ret = self.one()
2843 if not isinstance(ret, collections_abc.Sequence):
2844 return ret
2845 return ret[0]
2846 except sa_exc.NoResultFound:
2847 return None
2848
2849 def __iter__(self) -> Iterator[_T]:
2850 result = self._iter()

Callers 15

has_schemaMethod · 0.45
has_tableMethod · 0.45
has_sequenceMethod · 0.45
has_typeMethod · 0.45
get_table_oidMethod · 0.45
get_view_definitionMethod · 0.45
_pg_create_dbFunction · 0.45
has_tableMethod · 0.45

Calls 1

oneMethod · 0.95