What is the difference between .one() and .scalar()
Question:
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
Answers:
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.
Here is the difference and when to use each:
When to use one()
:
If you have a query that should return 1 result, otherwise raise an exception – even if it returns 0 results. In other words, it does not allow empty results
.
When to use scalar():
If you have a query that either returns 1 result or no results. Otherwise throw an exception. In other words, it does allow empty results
.
In SQLAlchemy 2.0 style, the result format is also different.
one()
returns Row
object (named-tuple):
>>> session.execute(select(Note)).one()
(<Note>,)
while scalar()
returns a single element (the first element of the Row object/tuple):
<Note>
If you want to get the single element with one()
, you can call it with scalars()
:
>>> session.execute(select(Note)).scalars().one()
See the 2.0 migration docs for more details: https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-orm-usage
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.
Here is the difference and when to use each:
When to use one()
:
If you have a query that should return 1 result, otherwise raise an exception – even if it returns 0 results. In other words, it does not allow empty results
.
When to use scalar():
If you have a query that either returns 1 result or no results. Otherwise throw an exception. In other words, it does allow empty results
.
In SQLAlchemy 2.0 style, the result format is also different.
one()
returns Row
object (named-tuple):
>>> session.execute(select(Note)).one()
(<Note>,)
while scalar()
returns a single element (the first element of the Row object/tuple):
<Note>
If you want to get the single element with one()
, you can call it with scalars()
:
>>> session.execute(select(Note)).scalars().one()
See the 2.0 migration docs for more details: https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-orm-usage