What is the main difference between .one()
and .scalar()
in SQLAlchemy, as both doing the same jobs.
I saw some sites like tutorialpoint.com but that explanation is not enough for me to understand clearly
SQLAlchemy has nice documentation.
one()
Return exactly one result or raise an exception.
Raises sqlalchemy.orm.exc.NoResultFound if the query selects no rows. Raises sqlalchemy.orm.exc.MultipleResultsFound if multiple object identities are returned, or if multiple rows are returned for a query that returns only scalar values as opposed to full identity-mapped entities.
Link on one() method
scalar()
Return the first element of the first result or None if no rows present. If multiple rows are returned, raises MultipleResultsFound.
Link on scalar() method.
and if you will have some questions related to SQLAlchemy my recommendation - first of all, to check the documentation, since it's really powerful and clean.
UPD: I've updated the initial answer since it contains links to the SQLAlchemy v1. Please see the links under, that contain links to the v2.