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 :

Vous êtes un professionnel et vous avez besoin d'une formation ? Calcul scientifique
avec Python
Voir le programme détaillé
Module « sqlalchemy.orm »

Fonction aliased - module sqlalchemy.orm

Signature de la fonction aliased

def aliased(element: 'Union[_EntityType[_O], FromClause]', alias: 'Optional[FromClause]' = None, name: 'Optional[str]' = None, flat: 'bool' = False, adapt_on_names: 'bool' = False) -> 'Union[AliasedClass[_O], FromClause, AliasedType[_O]]' 

Description

help(sqlalchemy.orm.aliased)

Produce an alias of the given element, usually an :class:`.AliasedClass`
instance.

E.g.::

    my_alias = aliased(MyClass)

    stmt = select(MyClass, my_alias).filter(MyClass.id > my_alias.id)
    result = session.execute(stmt)

The :func:`.aliased` function is used to create an ad-hoc mapping of a
mapped class to a new selectable.  By default, a selectable is generated
from the normally mapped selectable (typically a :class:`_schema.Table`
) using the
:meth:`_expression.FromClause.alias` method. However, :func:`.aliased`
can also be
used to link the class to a new :func:`_expression.select` statement.
Also, the :func:`.with_polymorphic` function is a variant of
:func:`.aliased` that is intended to specify a so-called "polymorphic
selectable", that corresponds to the union of several joined-inheritance
subclasses at once.

For convenience, the :func:`.aliased` function also accepts plain
:class:`_expression.FromClause` constructs, such as a
:class:`_schema.Table` or
:func:`_expression.select` construct.   In those cases, the
:meth:`_expression.FromClause.alias`
method is called on the object and the new
:class:`_expression.Alias` object returned.  The returned
:class:`_expression.Alias` is not
ORM-mapped in this case.

.. seealso::

    :ref:`tutorial_orm_entity_aliases` - in the :ref:`unified_tutorial`

    :ref:`orm_queryguide_orm_aliases` - in the :ref:`queryguide_toplevel`

:param element: element to be aliased.  Is normally a mapped class,
 but for convenience can also be a :class:`_expression.FromClause`
 element.

:param alias: Optional selectable unit to map the element to.  This is
 usually used to link the object to a subquery, and should be an aliased
 select construct as one would produce from the
 :meth:`_query.Query.subquery` method or
 the :meth:`_expression.Select.subquery` or
 :meth:`_expression.Select.alias` methods of the :func:`_expression.select`
 construct.

:param name: optional string name to use for the alias, if not specified
 by the ``alias`` parameter.  The name, among other things, forms the
 attribute name that will be accessible via tuples returned by a
 :class:`_query.Query` object.  Not supported when creating aliases
 of :class:`_sql.Join` objects.

:param flat: Boolean, will be passed through to the
 :meth:`_expression.FromClause.alias` call so that aliases of
 :class:`_expression.Join` objects will alias the individual tables
 inside the join, rather than creating a subquery.  This is generally
 supported by all modern databases with regards to right-nested joins
 and generally produces more efficient queries.

 When :paramref:`_orm.aliased.flat` is combined with
 :paramref:`_orm.aliased.name`, the resulting joins will alias individual
 tables using a naming scheme similar to ``<prefix>_<tablename>``.  This
 naming scheme is for visibility / debugging purposes only and the
 specific scheme is subject to change without notice.

 .. versionadded:: 2.0.32 added support for combining
    :paramref:`_orm.aliased.name` with :paramref:`_orm.aliased.flat`.
    Previously, this would raise ``NotImplementedError``.

:param adapt_on_names: if True, more liberal "matching" will be used when
 mapping the mapped columns of the ORM entity to those of the
 given selectable - a name-based match will be performed if the
 given selectable doesn't otherwise have a column that corresponds
 to one on the entity.  The use case for this is when associating
 an entity with some derived selectable such as one that uses
 aggregate functions::

    class UnitPrice(Base):
        __tablename__ = "unit_price"
        ...
        unit_id = Column(Integer)
        price = Column(Numeric)


    aggregated_unit_price = (
        Session.query(func.sum(UnitPrice.price).label("price"))
        .group_by(UnitPrice.unit_id)
        .subquery()
    )

    aggregated_unit_price = aliased(
        UnitPrice, alias=aggregated_unit_price, adapt_on_names=True
    )

 Above, functions on ``aggregated_unit_price`` which refer to
 ``.price`` will return the
 ``func.sum(UnitPrice.price).label('price')`` column, as it is
 matched on the name "price".  Ordinarily, the "price" function
 wouldn't have any "column correspondence" to the actual
 ``UnitPrice.price`` column as it is not a proxy of the original.



Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé