MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / scalar

Method scalar

lib/sqlalchemy/orm/query.py:2874–2904  ·  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() <Item> >>> session.query(Item.id).scalar() 1 >>> ses

(self)

Source from the content-addressed store, hash-verified

2872 return self._iter().one() # type: ignore
2873
2874 def scalar(self) -> Any:
2875 """Return the first element of the first result or None
2876 if no rows present. If multiple rows are returned,
2877 raises :class:`_exc.MultipleResultsFound`.
2878
2879 >>> session.query(Item).scalar()
2880 <Item>
2881 >>> session.query(Item.id).scalar()
2882 1
2883 >>> session.query(Item.id).filter(Item.id < 0).scalar()
2884 None
2885 >>> session.query(Item.id, Item.name).scalar()
2886 1
2887 >>> session.query(func.count(Parent.id)).scalar()
2888 20
2889
2890 This results in an execution of the underlying query.
2891
2892 .. seealso::
2893
2894 :meth:`_engine.Result.scalar` - v2 comparable method.
2895
2896 """
2897 # TODO: not sure why we can't use result.scalar() here
2898 try:
2899 ret = self.one()
2900 if not isinstance(ret, collections_abc.Sequence):
2901 return ret
2902 return ret[0]
2903 except sa_exc.NoResultFound:
2904 return None
2905
2906 def __iter__(self) -> Iterator[_T]:
2907 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
_load_pg_am_btree_oidMethod · 0.45
_pg_create_dbFunction · 0.45

Calls 1

oneMethod · 0.95