MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / with_polymorphic

Function with_polymorphic

lib/sqlalchemy/orm/_orm_constructors.py:2490–2587  ·  view source on GitHub ↗

Produce an :class:`.AliasedClass` construct which specifies columns for descendant mappers of the given base. Using this method will ensure that each descendant mapper's tables are included in the FROM clause, and will allow filter() criterion to be used against those tables. The r

(
    base: Union[Type[_O], Mapper[_O]],
    classes: Union[Literal["*"], Iterable[Type[Any]]],
    selectable: Union[Literal[False, None], FromClause] = False,
    flat: bool = False,
    polymorphic_on: Optional[ColumnElement[Any]] = None,
    aliased: bool = False,
    innerjoin: bool = False,
    adapt_on_names: bool = False,
    name: Optional[str] = None,
    _use_mapper_path: bool = False,
)

Source from the content-addressed store, hash-verified

2488
2489
2490def with_polymorphic(
2491 base: Union[Type[_O], Mapper[_O]],
2492 classes: Union[Literal["*"], Iterable[Type[Any]]],
2493 selectable: Union[Literal[False, None], FromClause] = False,
2494 flat: bool = False,
2495 polymorphic_on: Optional[ColumnElement[Any]] = None,
2496 aliased: bool = False,
2497 innerjoin: bool = False,
2498 adapt_on_names: bool = False,
2499 name: Optional[str] = None,
2500 _use_mapper_path: bool = False,
2501) -> AliasedClass[_O]:
2502 """Produce an :class:`.AliasedClass` construct which specifies
2503 columns for descendant mappers of the given base.
2504
2505 Using this method will ensure that each descendant mapper's
2506 tables are included in the FROM clause, and will allow filter()
2507 criterion to be used against those tables. The resulting
2508 instances will also have those columns already loaded so that
2509 no "post fetch" of those columns will be required.
2510
2511 .. seealso::
2512
2513 :ref:`with_polymorphic` - full discussion of
2514 :func:`_orm.with_polymorphic`.
2515
2516 :param base: Base class to be aliased.
2517
2518 :param classes: a single class or mapper, or list of
2519 class/mappers, which inherit from the base class.
2520 Alternatively, it may also be the string ``'*'``, in which case
2521 all descending mapped classes will be added to the FROM clause.
2522
2523 :param aliased: when True, the selectable will be aliased. For a
2524 JOIN, this means the JOIN will be SELECTed from inside of a subquery
2525 unless the :paramref:`_orm.with_polymorphic.flat` flag is set to
2526 True, which is recommended for simpler use cases.
2527
2528 :param flat: Boolean, will be passed through to the
2529 :meth:`_expression.FromClause.alias` call so that aliases of
2530 :class:`_expression.Join` objects will alias the individual tables
2531 inside the join, rather than creating a subquery. This is generally
2532 supported by all modern databases with regards to right-nested joins
2533 and generally produces more efficient queries. Setting this flag is
2534 recommended as long as the resulting SQL is functional.
2535
2536 :param selectable: a table or subquery that will
2537 be used in place of the generated FROM clause. This argument is
2538 required if any of the desired classes use concrete table
2539 inheritance, since SQLAlchemy currently cannot generate UNIONs
2540 among tables automatically. If used, the ``selectable`` argument
2541 must represent the full set of tables and columns mapped by every
2542 mapped class. Otherwise, the unaccounted mapped columns will
2543 result in their table being appended directly to the FROM clause
2544 which will usually lead to incorrect results.
2545
2546 When left at its default value of ``False``, the polymorphic
2547 selectable assigned to the base mapper is used for selecting rows.

Calls 1