Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Classe « Query »

Méthode sqlalchemy.orm.Query.select_entity_from

Signature de la méthode select_entity_from

def select_entity_from(self, from_obj) 

Description

select_entity_from.__doc__

Set the FROM clause of this :class:`_query.Query` to a
core selectable, applying it as a replacement FROM clause
for corresponding mapped entities.

.. deprecated:: 1.4 The :meth:`_orm.Query.select_entity_from` method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Use the :func:`_orm.aliased` construct instead (Background on SQLAlchemy 2.0 at: :ref:`migration_20_toplevel`)

The :meth:`_query.Query.select_entity_from`
method supplies an alternative
approach to the use case of applying an :func:`.aliased` construct
explicitly throughout a query.  Instead of referring to the
:func:`.aliased` construct explicitly,
:meth:`_query.Query.select_entity_from` automatically *adapts* all
occurrences of the entity to the target selectable.

Given a case for :func:`.aliased` such as selecting ``User``
objects from a SELECT statement::

    select_stmt = select(User).where(User.id == 7)
    user_alias = aliased(User, select_stmt)

    q = session.query(user_alias).\
        filter(user_alias.name == 'ed')

Above, we apply the ``user_alias`` object explicitly throughout the
query.  When it's not feasible for ``user_alias`` to be referenced
explicitly in many places, :meth:`_query.Query.select_entity_from`
may be
used at the start of the query to adapt the existing ``User`` entity::

    q = session.query(User).\
        select_entity_from(select_stmt.subquery()).\
        filter(User.name == 'ed')

Above, the generated SQL will show that the ``User`` entity is
adapted to our statement, even in the case of the WHERE clause:

.. sourcecode:: sql

    SELECT anon_1.id AS anon_1_id, anon_1.name AS anon_1_name
    FROM (SELECT "user".id AS id, "user".name AS name
    FROM "user"
    WHERE "user".id = :id_1) AS anon_1
    WHERE anon_1.name = :name_1

The :meth:`_query.Query.select_entity_from` method is similar to the
:meth:`_query.Query.select_from` method,
in that it sets the FROM clause
of the query.  The difference is that it additionally applies
adaptation to the other parts of the query that refer to the
primary entity.  If above we had used :meth:`_query.Query.select_from`
instead, the SQL generated would have been:

.. sourcecode:: sql

    -- uses plain select_from(), not select_entity_from()
    SELECT "user".id AS user_id, "user".name AS user_name
    FROM "user", (SELECT "user".id AS id, "user".name AS name
    FROM "user"
    WHERE "user".id = :id_1) AS anon_1
    WHERE "user".name = :name_1

To supply textual SQL to the :meth:`_query.Query.select_entity_from`
method,
we can make use of the :func:`_expression.text` construct.  However,
the
:func:`_expression.text`
construct needs to be aligned with the columns of our
entity, which is achieved by making use of the
:meth:`_expression.TextClause.columns` method::

    text_stmt = text("select id, name from user").columns(
        User.id, User.name).subquery()
    q = session.query(User).select_entity_from(text_stmt)

:meth:`_query.Query.select_entity_from` itself accepts an
:func:`.aliased`
object, so that the special options of :func:`.aliased` such as
:paramref:`.aliased.adapt_on_names` may be used within the
scope of the :meth:`_query.Query.select_entity_from`
method's adaptation
services.  Suppose
a view ``user_view`` also returns rows from ``user``.    If
we reflect this view into a :class:`_schema.Table`, this view has no
relationship to the :class:`_schema.Table` to which we are mapped,
however
we can use name matching to select from it::

    user_view = Table('user_view', metadata,
                      autoload_with=engine)
    user_view_alias = aliased(
        User, user_view, adapt_on_names=True)
    q = session.query(User).\
        select_entity_from(user_view_alias).\
        order_by(User.name)

.. versionchanged:: 1.1.7 The :meth:`_query.Query.select_entity_from`
   method now accepts an :func:`.aliased` object as an alternative
   to a :class:`_expression.FromClause` object.

:param from_obj: a :class:`_expression.FromClause`
 object that will replace
 the FROM clause of this :class:`_query.Query`.
 It also may be an instance
 of :func:`.aliased`.



.. seealso::

    :meth:`_query.Query.select_from`